window.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. 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. } else {
  177. w.overTop = false
  178. }
  179. // Check if cursor is on the bottom of the window (border + drag margin)
  180. if cy >= w.height-w.borderSizes.Bottom-w.dragPadding {
  181. w.overBottom = true
  182. } else {
  183. w.overBottom = false
  184. }
  185. // Check if cursor is on the left of the window (border + drag margin)
  186. if cx <= w.borderSizes.Left+w.dragPadding {
  187. w.overLeft = true
  188. } else {
  189. w.overLeft = false
  190. }
  191. // Check if cursor is on the right of the window (border + drag margin)
  192. if cx >= w.width-w.borderSizes.Right-w.dragPadding {
  193. w.overRight = true
  194. } else {
  195. w.overRight = false
  196. }
  197. // Update cursor image based on cursor position
  198. if (w.overTop || w.overBottom) && !w.overRight && !w.overLeft {
  199. window.Get().SetCursor(window.VResizeCursor)
  200. } else if (w.overRight || w.overLeft) && !w.overTop && !w.overBottom {
  201. window.Get().SetCursor(window.HResizeCursor)
  202. } else if (w.overRight && w.overTop) || (w.overBottom && w.overLeft) {
  203. window.Get().SetCursor(window.DiagResize1Cursor)
  204. } else if (w.overRight && w.overBottom) || (w.overTop && w.overLeft) {
  205. window.Get().SetCursor(window.DiagResize2Cursor)
  206. }
  207. // If cursor is not near the border of the window then reset the cursor
  208. if !w.overTop && !w.overRight && !w.overBottom && !w.overLeft {
  209. window.Get().SetCursor(window.ArrowCursor)
  210. }
  211. }
  212. } else if evname == OnCursorLeave {
  213. window.Get().SetCursor(window.ArrowCursor)
  214. w.drag = false
  215. }
  216. }
  217. // update updates the window's visual state.
  218. func (w *Window) update() {
  219. if !w.Enabled() {
  220. w.applyStyle(&w.styles.Disabled)
  221. return
  222. }
  223. w.applyStyle(&w.styles.Normal)
  224. }
  225. // applyStyle applies a window style to the window.
  226. func (w *Window) applyStyle(s *WindowStyle) {
  227. w.SetBordersColor4(&s.BorderColor)
  228. w.SetBordersFrom(&s.Border)
  229. w.SetPaddingsFrom(&s.Padding)
  230. w.client.SetMarginsFrom(&s.Margin)
  231. w.client.SetColor4(&s.BgColor)
  232. if w.title != nil {
  233. w.title.applyStyle(&s.TitleStyle)
  234. }
  235. }
  236. // recalc recalculates the sizes and positions of the internal panels
  237. // from the outside to the inside.
  238. func (w *Window) recalc() {
  239. // Window title
  240. height := w.content.Height
  241. width := w.content.Width
  242. cx := float32(0)
  243. cy := float32(0)
  244. if w.title != nil {
  245. w.title.SetWidth(w.content.Width)
  246. w.title.recalc()
  247. height -= w.title.height
  248. cy = w.title.height
  249. }
  250. // Content area
  251. w.client.SetPosition(cx, cy)
  252. w.client.SetSize(width, height)
  253. }
  254. // WindowTitle represents the title bar of a Window
  255. type WindowTitle struct {
  256. Panel // Embedded panel
  257. win *Window // Window to which this title belongs
  258. label Label // Label for the title
  259. pressed bool // Whether the left mouse button is pressed
  260. closeButton *Button // The close button on the top right corner
  261. closeButtonVisible bool // Whether the close button is present
  262. // Last mouse coordinates
  263. mouseX float32
  264. mouseY float32
  265. }
  266. // WindowTitleStyle contains the styling for a window title.
  267. type WindowTitleStyle struct {
  268. PanelStyle
  269. FgColor math32.Color4
  270. }
  271. // newWindowTitle creates and returns a pointer to a window title panel.
  272. func newWindowTitle(win *Window, text string) *WindowTitle {
  273. wt := new(WindowTitle)
  274. wt.win = win
  275. wt.Panel.Initialize(wt, 0, 0)
  276. wt.label.initialize(text, StyleDefault().Font)
  277. wt.Panel.Add(&wt.label)
  278. wt.closeButton = NewButton("")
  279. wt.closeButton.SetIcon(icon.Close)
  280. wt.closeButton.Subscribe(OnCursorEnter, func(s string, i interface{}) {
  281. window.Get().SetCursor(window.ArrowCursor)
  282. })
  283. wt.closeButton.Subscribe(OnClick, func(s string, i interface{}) {
  284. wt.win.Parent().GetNode().Remove(wt.win)
  285. wt.win.Dispose()
  286. wt.win.Dispatch("gui.OnWindowClose", nil)
  287. })
  288. wt.Panel.Add(wt.closeButton)
  289. wt.closeButtonVisible = true
  290. wt.Subscribe(OnMouseDown, wt.onMouse)
  291. wt.Subscribe(OnMouseUp, wt.onMouse)
  292. wt.Subscribe(OnCursor, wt.onCursor)
  293. wt.Subscribe(OnCursorEnter, wt.onCursor)
  294. wt.Subscribe(OnCursorLeave, wt.onCursor)
  295. wt.recalc()
  296. return wt
  297. }
  298. // setCloseButton sets whether the close button is present on the top right corner.
  299. func (wt *WindowTitle) setCloseButton(state bool) {
  300. if state {
  301. wt.closeButtonVisible = true
  302. wt.Panel.Add(wt.closeButton)
  303. } else {
  304. wt.closeButtonVisible = false
  305. wt.Panel.Remove(wt.closeButton)
  306. }
  307. }
  308. // onMouse process subscribed mouse button events over the window title.
  309. func (wt *WindowTitle) onMouse(evname string, ev interface{}) {
  310. mev := ev.(*window.MouseEvent)
  311. switch evname {
  312. case OnMouseDown:
  313. wt.pressed = true
  314. wt.mouseX = mev.Xpos
  315. wt.mouseY = mev.Ypos
  316. Manager().SetCursorFocus(wt)
  317. case OnMouseUp:
  318. wt.pressed = false
  319. Manager().SetCursorFocus(nil)
  320. default:
  321. return
  322. }
  323. }
  324. // onCursor process subscribed cursor events over the window title.
  325. func (wt *WindowTitle) onCursor(evname string, ev interface{}) {
  326. if evname == OnCursorLeave {
  327. window.Get().SetCursor(window.ArrowCursor)
  328. wt.pressed = false
  329. } else if evname == OnCursor {
  330. if !wt.pressed {
  331. return
  332. }
  333. cev := ev.(*window.CursorEvent)
  334. dy := wt.mouseY - cev.Ypos
  335. dx := wt.mouseX - cev.Xpos
  336. wt.mouseX = cev.Xpos
  337. wt.mouseY = cev.Ypos
  338. posX := wt.win.Position().X - dx
  339. posY := wt.win.Position().Y - dy
  340. wt.win.SetPosition(posX, posY)
  341. }
  342. }
  343. // applyStyle applies the specified WindowTitleStyle.
  344. func (wt *WindowTitle) applyStyle(s *WindowTitleStyle) {
  345. wt.Panel.ApplyStyle(&s.PanelStyle)
  346. wt.label.SetColor4(&s.FgColor)
  347. }
  348. // recalc recalculates the height and position of the label in the title bar.
  349. func (wt *WindowTitle) recalc() {
  350. xpos := (wt.width - wt.label.width) / 2
  351. wt.label.SetPositionX(xpos)
  352. wt.SetContentHeight(wt.closeButton.Height())
  353. if wt.closeButtonVisible {
  354. wt.closeButton.SetPositionX(wt.width - wt.closeButton.width)
  355. }
  356. }