window.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gui
  5. import (
  6. "github.com/g3n/engine/gui/assets/icon"
  7. "github.com/g3n/engine/math32"
  8. "github.com/g3n/engine/window"
  9. )
  10. /*********************************************
  11. Window panel
  12. +-------------------------------------+---+
  13. | Title panel | X |
  14. +-------------------------------------+---+
  15. | Content panel |
  16. | +-----------------------------------+ |
  17. | | | |
  18. | | | |
  19. | | | |
  20. | | | |
  21. | | | |
  22. | | | |
  23. | +-----------------------------------+ |
  24. | |
  25. +-----------------------------------------+
  26. *********************************************/
  27. // Window represents a window GUI element
  28. type Window struct {
  29. Panel // Embedded Panel
  30. styles *WindowStyles
  31. title *WindowTitle // internal optional title panel
  32. client Panel // internal client panel
  33. resizable bool // Specifies whether the window is resizable
  34. drag bool // Whether the mouse buttons is pressed (i.e. when dragging)
  35. dragPadding float32 // Extra width used to resize (in addition to border sizes)
  36. // To keep track of which window borders the cursor is over
  37. overTop bool
  38. overRight bool
  39. overBottom bool
  40. overLeft bool
  41. // Minimum and maximum sizes
  42. minSize math32.Vector2
  43. maxSize math32.Vector2
  44. }
  45. // WindowStyle contains the styling of a Window
  46. type WindowStyle struct {
  47. PanelStyle
  48. TitleStyle WindowTitleStyle
  49. }
  50. // WindowStyles contains a WindowStyle for each valid GUI state
  51. type WindowStyles struct {
  52. Normal WindowStyle
  53. Over WindowStyle
  54. Focus WindowStyle
  55. Disabled WindowStyle
  56. }
  57. // NewWindow creates and returns a pointer to a new window with the
  58. // specified dimensions
  59. func NewWindow(width, height float32) *Window {
  60. w := new(Window)
  61. w.styles = &StyleDefault().Window
  62. w.Panel.Initialize(w, width, height)
  63. w.Panel.Subscribe(OnMouseDown, w.onMouse)
  64. w.Panel.Subscribe(OnMouseUp, w.onMouse)
  65. w.Panel.Subscribe(OnCursor, w.onCursor)
  66. w.Panel.Subscribe(OnCursorEnter, w.onCursor)
  67. w.Panel.Subscribe(OnCursorLeave, w.onCursor)
  68. w.Panel.Subscribe(OnResize, func(evname string, ev interface{}) { w.recalc() })
  69. w.client.Initialize(&w.client, 0, 0)
  70. w.Panel.Add(&w.client)
  71. w.dragPadding = 5
  72. w.recalc()
  73. w.update()
  74. return w
  75. }
  76. // SetResizable sets whether the window is resizable.
  77. func (w *Window) SetResizable(state bool) {
  78. w.resizable = state
  79. }
  80. // SetCloseButton sets whether the window has a close button on the top right.
  81. func (w *Window) SetCloseButton(state bool) {
  82. w.title.setCloseButton(state)
  83. }
  84. // SetTitle sets the title of the window.
  85. func (w *Window) SetTitle(text string) {
  86. if w.title == nil {
  87. w.title = newWindowTitle(w, text)
  88. w.title.Subscribe(OnCursor, w.onCursor)
  89. w.Panel.Add(w.title)
  90. } else {
  91. w.title.label.SetText(text)
  92. }
  93. w.update()
  94. w.recalc()
  95. }
  96. // Add adds a child panel to the client area of this window
  97. func (w *Window) Add(ichild IPanel) *Window {
  98. w.client.Add(ichild)
  99. return w
  100. }
  101. // SetLayout sets the layout of the client panel.
  102. func (w *Window) SetLayout(layout ILayout) {
  103. w.client.SetLayout(layout)
  104. }
  105. // onMouse process subscribed mouse events over the window
  106. func (w *Window) onMouse(evname string, ev interface{}) {
  107. switch evname {
  108. case OnMouseDown:
  109. // Move the window above everything contained in its parent
  110. parent := w.Parent().(IPanel).GetPanel()
  111. parent.SetTopChild(w)
  112. // If the click happened inside the draggable area, then set drag to true
  113. if w.overTop || w.overRight || w.overBottom || w.overLeft {
  114. w.drag = true
  115. Manager().SetCursorFocus(w)
  116. }
  117. case OnMouseUp:
  118. w.drag = false
  119. Manager().SetCursorFocus(nil)
  120. default:
  121. return
  122. }
  123. }
  124. // onCursor process subscribed cursor events over the window
  125. func (w *Window) onCursor(evname string, ev interface{}) {
  126. // If the window is not resizable we are not interested in cursor movements
  127. if !w.resizable {
  128. return
  129. }
  130. if evname == OnCursor {
  131. cev := ev.(*window.CursorEvent)
  132. // If already dragging - update window size and position depending
  133. // on the cursor position and the borders being dragged
  134. if w.drag {
  135. titleHeight := float32(0)
  136. titleLabelWidth := float32(0)
  137. titleCloseBtnWidth := float32(0)
  138. if w.title != nil {
  139. titleHeight = w.title.height
  140. titleLabelWidth = w.title.label.Width()
  141. if w.title.closeButton != nil {
  142. titleCloseBtnWidth = w.title.closeButton.Width()
  143. }
  144. }
  145. if w.overTop {
  146. delta := cev.Ypos - w.pospix.Y
  147. newHeight := w.Height() - delta
  148. minHeight := titleHeight
  149. if newHeight >= minHeight {
  150. w.SetPositionY(w.Position().Y + delta)
  151. w.SetHeight(math32.Max(newHeight, minHeight))
  152. } else {
  153. w.SetPositionY(w.Position().Y + w.Height() - minHeight)
  154. w.SetHeight(titleHeight)
  155. }
  156. }
  157. if w.overRight {
  158. delta := cev.Xpos - (w.pospix.X + w.width)
  159. newWidth := w.Width() + delta
  160. w.SetWidth(math32.Max(newWidth, titleLabelWidth+titleCloseBtnWidth))
  161. }
  162. if w.overBottom {
  163. delta := cev.Ypos - (w.pospix.Y + w.height)
  164. newHeight := w.Height() + delta
  165. w.SetHeight(math32.Max(newHeight, titleHeight))
  166. }
  167. if w.overLeft {
  168. delta := cev.Xpos - w.pospix.X
  169. newWidth := w.Width() - delta
  170. minWidth := titleLabelWidth + titleCloseBtnWidth
  171. if newWidth >= minWidth {
  172. w.SetPositionX(w.Position().X + delta)
  173. w.SetWidth(math32.Max(newWidth, minWidth))
  174. } else {
  175. w.SetPositionX(w.Position().X + w.Width() - minWidth)
  176. w.SetWidth(minWidth)
  177. }
  178. }
  179. } else {
  180. // Obtain cursor position relative to window
  181. cx := cev.Xpos - w.pospix.X
  182. cy := cev.Ypos - w.pospix.Y
  183. // Check if cursor is on the top of the window (border + drag margin)
  184. if cy <= w.borderSizes.Top {
  185. w.overTop = true
  186. } else {
  187. w.overTop = false
  188. }
  189. // Check if cursor is on the bottom of the window (border + drag margin)
  190. if cy >= w.height-w.borderSizes.Bottom-w.dragPadding {
  191. w.overBottom = true
  192. } else {
  193. w.overBottom = false
  194. }
  195. // Check if cursor is on the left of the window (border + drag margin)
  196. if cx <= w.borderSizes.Left+w.dragPadding {
  197. w.overLeft = true
  198. } else {
  199. w.overLeft = false
  200. }
  201. // Check if cursor is on the right of the window (border + drag margin)
  202. if cx >= w.width-w.borderSizes.Right-w.dragPadding {
  203. w.overRight = true
  204. } else {
  205. w.overRight = false
  206. }
  207. // Update cursor image based on cursor position
  208. if (w.overTop || w.overBottom) && !w.overRight && !w.overLeft {
  209. window.Get().SetCursor(window.VResizeCursor)
  210. } else if (w.overRight || w.overLeft) && !w.overTop && !w.overBottom {
  211. window.Get().SetCursor(window.HResizeCursor)
  212. } else if (w.overRight && w.overTop) || (w.overBottom && w.overLeft) {
  213. window.Get().SetCursor(window.DiagResize1Cursor)
  214. } else if (w.overRight && w.overBottom) || (w.overTop && w.overLeft) {
  215. window.Get().SetCursor(window.DiagResize2Cursor)
  216. }
  217. // If cursor is not near the border of the window then reset the cursor
  218. if !w.overTop && !w.overRight && !w.overBottom && !w.overLeft {
  219. window.Get().SetCursor(window.ArrowCursor)
  220. }
  221. }
  222. } else if evname == OnCursorLeave {
  223. window.Get().SetCursor(window.ArrowCursor)
  224. w.drag = false
  225. }
  226. }
  227. // update updates the window's visual state.
  228. func (w *Window) update() {
  229. if !w.Enabled() {
  230. w.applyStyle(&w.styles.Disabled)
  231. return
  232. }
  233. w.applyStyle(&w.styles.Normal)
  234. }
  235. // applyStyle applies a window style to the window.
  236. func (w *Window) applyStyle(s *WindowStyle) {
  237. w.SetBordersColor4(&s.BorderColor)
  238. w.SetBordersFrom(&s.Border)
  239. w.SetPaddingsFrom(&s.Padding)
  240. w.client.SetMarginsFrom(&s.Margin)
  241. w.client.SetColor4(&s.BgColor)
  242. if w.title != nil {
  243. w.title.applyStyle(&s.TitleStyle)
  244. }
  245. }
  246. // recalc recalculates the sizes and positions of the internal panels
  247. // from the outside to the inside.
  248. func (w *Window) recalc() {
  249. // Window title
  250. height := w.content.Height
  251. width := w.content.Width
  252. cx := float32(0)
  253. cy := float32(0)
  254. if w.title != nil {
  255. w.title.SetWidth(w.content.Width)
  256. w.title.recalc()
  257. height -= w.title.height
  258. cy = w.title.height
  259. }
  260. // Content area
  261. w.client.SetPosition(cx, cy)
  262. w.client.SetSize(width, height)
  263. }
  264. // WindowTitle represents the title bar of a Window
  265. type WindowTitle struct {
  266. Panel // Embedded panel
  267. win *Window // Window to which this title belongs
  268. label Label // Label for the title
  269. pressed bool // Whether the left mouse button is pressed
  270. closeButton *Button // The close button on the top right corner
  271. closeButtonVisible bool // Whether the close button is present
  272. // Last mouse coordinates
  273. mouseX float32
  274. mouseY float32
  275. }
  276. // WindowTitleStyle contains the styling for a window title.
  277. type WindowTitleStyle struct {
  278. PanelStyle
  279. FgColor math32.Color4
  280. }
  281. // newWindowTitle creates and returns a pointer to a window title panel.
  282. func newWindowTitle(win *Window, text string) *WindowTitle {
  283. wt := new(WindowTitle)
  284. wt.win = win
  285. wt.Panel.Initialize(wt, 0, 0)
  286. wt.label.initialize(text, StyleDefault().Font)
  287. wt.Panel.Add(&wt.label)
  288. wt.closeButton = NewButton("")
  289. wt.closeButton.SetIcon(icon.Close)
  290. wt.closeButton.Subscribe(OnCursorEnter, func(s string, i interface{}) {
  291. window.Get().SetCursor(window.ArrowCursor)
  292. })
  293. wt.closeButton.Subscribe(OnClick, func(s string, i interface{}) {
  294. wt.win.Parent().GetNode().Remove(wt.win)
  295. wt.win.Dispose()
  296. wt.win.Dispatch("gui.OnWindowClose", nil)
  297. })
  298. wt.Panel.Add(wt.closeButton)
  299. wt.closeButtonVisible = true
  300. wt.Subscribe(OnMouseDown, wt.onMouse)
  301. wt.Subscribe(OnMouseUp, wt.onMouse)
  302. wt.Subscribe(OnCursor, wt.onCursor)
  303. wt.Subscribe(OnCursorEnter, wt.onCursor)
  304. wt.Subscribe(OnCursorLeave, wt.onCursor)
  305. wt.recalc()
  306. return wt
  307. }
  308. // setCloseButton sets whether the close button is present on the top right corner.
  309. func (wt *WindowTitle) setCloseButton(state bool) {
  310. if state {
  311. wt.closeButtonVisible = true
  312. wt.Panel.Add(wt.closeButton)
  313. } else {
  314. wt.closeButtonVisible = false
  315. wt.Panel.Remove(wt.closeButton)
  316. }
  317. }
  318. // onMouse process subscribed mouse button events over the window title.
  319. func (wt *WindowTitle) onMouse(evname string, ev interface{}) {
  320. mev := ev.(*window.MouseEvent)
  321. switch evname {
  322. case OnMouseDown:
  323. wt.pressed = true
  324. wt.mouseX = mev.Xpos
  325. wt.mouseY = mev.Ypos
  326. Manager().SetCursorFocus(wt)
  327. case OnMouseUp:
  328. wt.pressed = false
  329. Manager().SetCursorFocus(nil)
  330. default:
  331. return
  332. }
  333. }
  334. // onCursor process subscribed cursor events over the window title.
  335. func (wt *WindowTitle) onCursor(evname string, ev interface{}) {
  336. if evname == OnCursorLeave {
  337. window.Get().SetCursor(window.ArrowCursor)
  338. wt.pressed = false
  339. } else if evname == OnCursor {
  340. if !wt.pressed {
  341. return
  342. }
  343. cev := ev.(*window.CursorEvent)
  344. dy := wt.mouseY - cev.Ypos
  345. dx := wt.mouseX - cev.Xpos
  346. wt.mouseX = cev.Xpos
  347. wt.mouseY = cev.Ypos
  348. posX := wt.win.Position().X - dx
  349. posY := wt.win.Position().Y - dy
  350. wt.win.SetPosition(posX, posY)
  351. }
  352. }
  353. // applyStyle applies the specified WindowTitleStyle.
  354. func (wt *WindowTitle) applyStyle(s *WindowTitleStyle) {
  355. wt.Panel.ApplyStyle(&s.PanelStyle)
  356. wt.label.SetColor4(&s.FgColor)
  357. }
  358. // recalc recalculates the height and position of the label in the title bar.
  359. func (wt *WindowTitle) recalc() {
  360. xpos := (wt.width - wt.label.width) / 2
  361. wt.label.SetPositionX(xpos)
  362. wt.SetContentHeight(wt.closeButton.Height())
  363. if wt.closeButtonVisible {
  364. wt.closeButton.SetPositionX(wt.width - wt.closeButton.width)
  365. }
  366. }