window.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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/math32"
  7. "github.com/g3n/engine/window"
  8. "github.com/g3n/engine/gui/assets/icon"
  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(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(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.Panel.Add(w.title)
  89. } else {
  90. w.title.label.SetText(text)
  91. }
  92. w.update()
  93. w.recalc()
  94. }
  95. // Add adds a child panel to the client area of this window
  96. func (w *Window) Add(ichild IPanel) *Window {
  97. w.client.Add(ichild)
  98. return w
  99. }
  100. // SetLayout sets the layout of the client panel.
  101. func (w *Window) SetLayout(layout ILayout) {
  102. w.client.SetLayout(layout)
  103. }
  104. // onMouse process subscribed mouse events over the window
  105. func (w *Window) onMouse(evname string, ev interface{}) {
  106. switch evname {
  107. case OnMouseDown:
  108. // Move the window above everything contained in its parent
  109. parent := w.Parent().(IPanel).GetPanel()
  110. parent.SetTopChild(w)
  111. // If the click happened inside the draggable area, then set drag to true
  112. if w.overTop || w.overRight || w.overBottom || w.overLeft {
  113. w.drag = true
  114. w.root.SetMouseFocus(w)
  115. }
  116. case OnMouseUp:
  117. w.drag = false
  118. w.root.SetMouseFocus(nil)
  119. default:
  120. return
  121. }
  122. w.root.StopPropagation(StopAll)
  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. if w.overTop {
  136. delta := cev.Ypos - w.pospix.Y
  137. newHeight := w.Height() - delta
  138. minHeight := w.title.height
  139. if newHeight >= minHeight {
  140. w.SetPositionY(w.Position().Y + delta)
  141. w.SetHeight(math32.Max(newHeight, minHeight))
  142. } else {
  143. w.SetPositionY(w.Position().Y + w.Height() - minHeight)
  144. w.SetHeight(w.title.height)
  145. }
  146. }
  147. if w.overRight {
  148. delta := cev.Xpos - (w.pospix.X + w.width)
  149. newWidth := w.Width() + delta
  150. w.SetWidth(math32.Max(newWidth, w.title.label.Width() + w.title.closeButton.Width()))
  151. }
  152. if w.overBottom {
  153. delta := cev.Ypos - (w.pospix.Y + w.height)
  154. newHeight := w.Height() + delta
  155. w.SetHeight(math32.Max(newHeight, w.title.height))
  156. }
  157. if w.overLeft {
  158. delta := cev.Xpos - w.pospix.X
  159. newWidth := w.Width() - delta
  160. minWidth := w.title.label.Width() + w.title.closeButton.Width()
  161. if newWidth >= minWidth {
  162. w.SetPositionX(w.Position().X + delta)
  163. w.SetWidth(math32.Max(newWidth, minWidth))
  164. } else {
  165. w.SetPositionX(w.Position().X + w.Width() - minWidth)
  166. w.SetWidth(minWidth)
  167. }
  168. }
  169. } else {
  170. // Obtain cursor position relative to window
  171. cx := cev.Xpos - w.pospix.X
  172. cy := cev.Ypos - w.pospix.Y
  173. // Check if cursor is on the top of the window (border + drag margin)
  174. if cy <= w.borderSizes.Top {
  175. w.overTop = true
  176. w.root.SetCursorVResize()
  177. } else {
  178. w.overTop = false
  179. }
  180. // Check if cursor is on the bottom of the window (border + drag margin)
  181. if cy >= w.height-w.borderSizes.Bottom - w.dragPadding {
  182. w.overBottom = true
  183. } else {
  184. w.overBottom = false
  185. }
  186. // Check if cursor is on the left of the window (border + drag margin)
  187. if cx <= w.borderSizes.Left + w.dragPadding {
  188. w.overLeft = true
  189. w.root.SetCursorHResize()
  190. } else {
  191. w.overLeft = false
  192. }
  193. // Check if cursor is on the right of the window (border + drag margin)
  194. if cx >= w.width-w.borderSizes.Right - w.dragPadding {
  195. w.overRight = true
  196. w.root.SetCursorHResize()
  197. } else {
  198. w.overRight = false
  199. }
  200. // Update cursor image based on cursor position
  201. if (w.overTop || w.overBottom) && !w.overRight && !w.overLeft {
  202. w.root.SetCursorVResize()
  203. } else if (w.overRight || w.overLeft) && !w.overTop && !w.overBottom {
  204. w.root.SetCursorHResize()
  205. } else if (w.overRight && w.overTop) || (w.overBottom && w.overLeft) {
  206. w.root.SetCursorDiagResize1()
  207. } else if (w.overRight && w.overBottom) || (w.overTop && w.overLeft) {
  208. w.root.SetCursorDiagResize2()
  209. }
  210. // If cursor is not near the border of the window then reset the cursor
  211. if !w.overTop && !w.overRight && !w.overBottom && !w.overLeft {
  212. w.root.SetCursorNormal()
  213. }
  214. }
  215. } else if evname == OnCursorLeave {
  216. w.root.SetCursorNormal()
  217. w.drag = false
  218. }
  219. w.root.StopPropagation(StopAll)
  220. }
  221. // update updates the window's visual state.
  222. func (w *Window) update() {
  223. if !w.Enabled() {
  224. w.applyStyle(&w.styles.Disabled)
  225. return
  226. }
  227. w.applyStyle(&w.styles.Normal)
  228. }
  229. // applyStyle applies a window style to the window.
  230. func (w *Window) applyStyle(s *WindowStyle) {
  231. w.SetBordersColor4(&s.BorderColor)
  232. w.SetBordersFrom(&s.Border)
  233. w.SetPaddingsFrom(&s.Padding)
  234. w.client.SetMarginsFrom(&s.Margin)
  235. w.client.SetColor4(&s.BgColor)
  236. if w.title != nil {
  237. w.title.applyStyle(&s.TitleStyle)
  238. }
  239. }
  240. // recalc recalculates the sizes and positions of the internal panels
  241. // from the outside to the inside.
  242. func (w *Window) recalc() {
  243. // Window title
  244. height := w.content.Height
  245. width := w.content.Width
  246. cx := float32(0)
  247. cy := float32(0)
  248. if w.title != nil {
  249. w.title.SetWidth(w.content.Width)
  250. w.title.recalc()
  251. height -= w.title.height
  252. cy = w.title.height
  253. }
  254. // Content area
  255. w.client.SetPosition(cx, cy)
  256. w.client.SetSize(width, height)
  257. }
  258. // WindowTitle represents the title bar of a Window
  259. type WindowTitle struct {
  260. Panel // Embedded panel
  261. win *Window // Window to which this title belongs
  262. label Label // Label for the title
  263. pressed bool // Whether the left mouse button is pressed
  264. closeButton *Button // The close button on the top right corner
  265. closeButtonVisible bool // Whether the close button is present
  266. // Last mouse coordinates
  267. mouseX float32
  268. mouseY float32
  269. }
  270. // WindowTitleStyle contains the styling for a window title.
  271. type WindowTitleStyle struct {
  272. PanelStyle
  273. FgColor math32.Color4
  274. }
  275. // newWindowTitle creates and returns a pointer to a window title panel.
  276. func newWindowTitle(win *Window, text string) *WindowTitle {
  277. wt := new(WindowTitle)
  278. wt.win = win
  279. wt.Panel.Initialize(0, 0)
  280. wt.label.initialize(text, StyleDefault().Font)
  281. wt.Panel.Add(&wt.label)
  282. wt.closeButton = NewButton("")
  283. wt.closeButton.SetIcon(icon.Close)
  284. wt.closeButton.Subscribe(OnCursorEnter, func(s string, i interface{}) {
  285. wt.win.root.SetCursorNormal()
  286. })
  287. wt.closeButton.Subscribe(OnClick, func(s string, i interface{}) {
  288. wt.win.Parent().GetNode().Remove(wt.win)
  289. wt.win.Dispose()
  290. wt.win.Dispatch("gui.OnWindowClose", nil)
  291. })
  292. wt.Panel.Add(wt.closeButton)
  293. wt.closeButtonVisible = true
  294. wt.Subscribe(OnMouseDown, wt.onMouse)
  295. wt.Subscribe(OnMouseUp, wt.onMouse)
  296. wt.Subscribe(OnCursor, wt.onCursor)
  297. wt.Subscribe(OnCursorEnter, wt.onCursor)
  298. wt.Subscribe(OnCursorLeave, wt.onCursor)
  299. wt.recalc()
  300. return wt
  301. }
  302. // setCloseButton sets whether the close button is present on the top right corner.
  303. func (wt *WindowTitle) setCloseButton(state bool) {
  304. if state {
  305. wt.closeButtonVisible = true
  306. wt.Panel.Add(wt.closeButton)
  307. } else {
  308. wt.closeButtonVisible = false
  309. wt.Panel.Remove(wt.closeButton)
  310. }
  311. }
  312. // onMouse process subscribed mouse button events over the window title.
  313. func (wt *WindowTitle) onMouse(evname string, ev interface{}) {
  314. mev := ev.(*window.MouseEvent)
  315. switch evname {
  316. case OnMouseDown:
  317. wt.pressed = true
  318. wt.mouseX = mev.Xpos
  319. wt.mouseY = mev.Ypos
  320. wt.win.root.SetMouseFocus(wt)
  321. case OnMouseUp:
  322. wt.pressed = false
  323. wt.win.root.SetMouseFocus(nil)
  324. default:
  325. return
  326. }
  327. wt.win.root.StopPropagation(Stop3D)
  328. }
  329. // onCursor process subscribed cursor events over the window title.
  330. func (wt *WindowTitle) onCursor(evname string, ev interface{}) {
  331. if evname == OnCursorLeave {
  332. wt.win.root.SetCursorNormal()
  333. wt.pressed = false
  334. } else if evname == OnCursor {
  335. if !wt.pressed {
  336. wt.win.root.StopPropagation(Stop3D)
  337. return
  338. }
  339. cev := ev.(*window.CursorEvent)
  340. dy := wt.mouseY - cev.Ypos
  341. dx := wt.mouseX - cev.Xpos
  342. wt.mouseX = cev.Xpos
  343. wt.mouseY = cev.Ypos
  344. posX := wt.win.Position().X - dx
  345. posY := wt.win.Position().Y - dy
  346. wt.win.SetPosition(posX, posY)
  347. }
  348. wt.win.root.StopPropagation(Stop3D)
  349. }
  350. // applyStyle applies the specified WindowTitleStyle.
  351. func (wt *WindowTitle) applyStyle(s *WindowTitleStyle) {
  352. wt.Panel.ApplyStyle(&s.PanelStyle)
  353. wt.label.SetColor4(&s.FgColor)
  354. }
  355. // recalc recalculates the height and position of the label in the title bar.
  356. func (wt *WindowTitle) recalc() {
  357. xpos := (wt.width - wt.label.width) / 2
  358. wt.label.SetPositionX(xpos)
  359. wt.SetContentHeight(wt.closeButton.Height())
  360. if wt.closeButtonVisible {
  361. wt.closeButton.SetPositionX(wt.width - wt.closeButton.width)
  362. }
  363. }