window.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. )
  9. /*********************************************
  10. Window panel
  11. +-----------------------------------------+
  12. | Title panel |
  13. +-----------------------------------------+
  14. | Content panel |
  15. | +-----------------------------------+ |
  16. | | | |
  17. | | | |
  18. | | | |
  19. | | | |
  20. | | | |
  21. | | | |
  22. | +-----------------------------------+ |
  23. | |
  24. +-----------------------------------------+
  25. *********************************************/
  26. type Window struct {
  27. Panel // Embedded Panel
  28. styles *WindowStyles
  29. title *WindowTitle // internal optional title panel
  30. client Panel // internal client panel
  31. resizable Resizable
  32. overBorder string
  33. drag bool
  34. mouseX float32
  35. mouseY float32
  36. }
  37. type WindowStyle struct {
  38. Border BorderSizes
  39. Paddings BorderSizes
  40. BorderColor math32.Color4
  41. TitleBorders BorderSizes
  42. TitleBorderColor math32.Color4
  43. TitleBgColor math32.Color4
  44. TitleFgColor math32.Color4
  45. }
  46. // All Window styles
  47. type WindowStyles struct {
  48. Normal WindowStyle
  49. Over WindowStyle
  50. Focus WindowStyle
  51. Disabled WindowStyle
  52. }
  53. type Resizable int
  54. const (
  55. ResizeTop = Resizable(1 << (iota + 1))
  56. ResizeRight
  57. ResizeBottom
  58. ResizeLeft
  59. ResizeAll = ResizeTop | ResizeRight | ResizeBottom | ResizeLeft
  60. )
  61. // NewWindow creates and returns a pointer to a new window with the
  62. // specified dimensions
  63. func NewWindow(width, height float32) *Window {
  64. w := new(Window)
  65. w.styles = &StyleDefault.Window
  66. w.Panel.Initialize(width, height)
  67. w.Panel.Subscribe(OnMouseDown, w.onMouse)
  68. w.Panel.Subscribe(OnMouseUp, w.onMouse)
  69. w.Panel.Subscribe(OnCursor, w.onCursor)
  70. w.Panel.Subscribe(OnCursorEnter, w.onCursor)
  71. w.Panel.Subscribe(OnCursorLeave, w.onCursor)
  72. w.Panel.Subscribe(OnResize, func(evname string, ev interface{}) { w.recalc() })
  73. w.client.Initialize(0, 0)
  74. w.Panel.Add(&w.client)
  75. w.recalc()
  76. w.update()
  77. return w
  78. }
  79. // SetResizable set the borders which are resizable
  80. func (w *Window) SetResizable(res Resizable) {
  81. w.resizable = res
  82. }
  83. // SetTitle sets the title of this window
  84. func (w *Window) SetTitle(text string) {
  85. if w.title == nil {
  86. w.title = newWindowTitle(w, text)
  87. w.Panel.Add(w.title)
  88. } else {
  89. w.title.label.SetText(text)
  90. }
  91. w.update()
  92. w.recalc()
  93. }
  94. // Add adds a child panel to the client area of this window
  95. func (w *Window) Add(ichild IPanel) *Window {
  96. w.client.Add(ichild)
  97. return w
  98. }
  99. // SetLayout set the layout of this window content area
  100. func (w *Window) SetLayout(layout ILayout) *Window {
  101. w.client.SetLayout(layout)
  102. return w
  103. }
  104. // onMouse process subscribed mouse events over the window
  105. func (w *Window) onMouse(evname string, ev interface{}) {
  106. mev := ev.(*window.MouseEvent)
  107. switch evname {
  108. case OnMouseDown:
  109. w.SetForeground()
  110. if w.overBorder != "" {
  111. w.drag = true
  112. w.mouseX = mev.Xpos
  113. w.mouseY = mev.Ypos
  114. w.root.SetMouseFocus(w)
  115. }
  116. case OnMouseUp:
  117. w.drag = false
  118. w.root.SetCursorNormal()
  119. w.root.SetMouseFocus(nil)
  120. default:
  121. return
  122. }
  123. w.root.StopPropagation(StopAll)
  124. }
  125. // onCursor process subscribed cursor events over the window
  126. func (w *Window) onCursor(evname string, ev interface{}) {
  127. cev := ev.(*window.CursorEvent)
  128. switch evname {
  129. case OnCursor:
  130. if !w.drag {
  131. cx := cev.Xpos - w.pospix.X
  132. cy := cev.Ypos - w.pospix.Y
  133. if cy <= w.borderSizes.Top {
  134. if w.resizable&ResizeTop != 0 {
  135. w.overBorder = "top"
  136. w.root.SetCursorVResize()
  137. }
  138. } else if cy >= w.height-w.borderSizes.Bottom {
  139. if w.resizable&ResizeBottom != 0 {
  140. w.overBorder = "bottom"
  141. w.root.SetCursorVResize()
  142. }
  143. } else if cx <= w.borderSizes.Left {
  144. if w.resizable&ResizeLeft != 0 {
  145. w.overBorder = "left"
  146. w.root.SetCursorHResize()
  147. }
  148. } else if cx >= w.width-w.borderSizes.Right {
  149. if w.resizable&ResizeRight != 0 {
  150. w.overBorder = "right"
  151. w.root.SetCursorHResize()
  152. }
  153. } else {
  154. if w.overBorder != "" {
  155. w.root.SetCursorNormal()
  156. w.overBorder = ""
  157. }
  158. }
  159. } else {
  160. switch w.overBorder {
  161. case "top":
  162. delta := cev.Ypos - w.mouseY
  163. w.mouseY = cev.Ypos
  164. newHeight := w.Height() - delta
  165. if newHeight < w.MinHeight() {
  166. return
  167. }
  168. w.SetPositionY(w.Position().Y + delta)
  169. w.SetHeight(newHeight)
  170. case "right":
  171. delta := cev.Xpos - w.mouseX
  172. w.mouseX = cev.Xpos
  173. newWidth := w.Width() + delta
  174. w.SetWidth(newWidth)
  175. case "bottom":
  176. delta := cev.Ypos - w.mouseY
  177. w.mouseY = cev.Ypos
  178. newHeight := w.Height() + delta
  179. w.SetHeight(newHeight)
  180. case "left":
  181. delta := cev.Xpos - w.mouseX
  182. w.mouseX = cev.Xpos
  183. newWidth := w.Width() - delta
  184. if newWidth < w.MinWidth() {
  185. return
  186. }
  187. w.SetPositionX(w.Position().X + delta)
  188. w.SetWidth(newWidth)
  189. }
  190. }
  191. case OnCursorLeave:
  192. if !w.drag {
  193. w.root.SetCursorNormal()
  194. }
  195. default:
  196. return
  197. }
  198. w.root.StopPropagation(StopAll)
  199. }
  200. // update updates the button visual state
  201. func (w *Window) update() {
  202. if !w.Enabled() {
  203. w.applyStyle(&w.styles.Disabled)
  204. return
  205. }
  206. w.applyStyle(&w.styles.Normal)
  207. }
  208. func (w *Window) applyStyle(s *WindowStyle) {
  209. w.SetBordersColor4(&s.BorderColor)
  210. w.SetBordersFrom(&s.Border)
  211. w.SetPaddingsFrom(&s.Paddings)
  212. if w.title != nil {
  213. w.title.applyStyle(s)
  214. }
  215. }
  216. // recalc recalculates the sizes and positions of the internal panels
  217. // from the outside to the inside.
  218. func (w *Window) recalc() {
  219. // Window title
  220. height := w.content.Height
  221. width := w.content.Width
  222. cx := float32(0)
  223. cy := float32(0)
  224. if w.title != nil {
  225. w.title.SetWidth(w.content.Width)
  226. w.title.recalc()
  227. height -= w.title.height
  228. cy = w.title.height
  229. }
  230. // Content area
  231. w.client.SetPosition(cx, cy)
  232. w.client.SetSize(width, height)
  233. }
  234. type WindowTitle struct {
  235. Panel // Embedded panel
  236. win *Window
  237. label Label
  238. pressed bool
  239. drag bool
  240. mouseX float32
  241. mouseY float32
  242. }
  243. // newWindowTitle creates and returns a pointer to a window title panel
  244. func newWindowTitle(win *Window, text string) *WindowTitle {
  245. wt := new(WindowTitle)
  246. wt.win = win
  247. wt.Panel.Initialize(0, 0)
  248. wt.label.initialize(text, StyleDefault.Font)
  249. wt.Panel.Add(&wt.label)
  250. wt.Subscribe(OnMouseDown, wt.onMouse)
  251. wt.Subscribe(OnMouseUp, wt.onMouse)
  252. wt.Subscribe(OnCursor, wt.onCursor)
  253. wt.Subscribe(OnCursorEnter, wt.onCursor)
  254. wt.Subscribe(OnCursorLeave, wt.onCursor)
  255. wt.recalc()
  256. return wt
  257. }
  258. // onMouse process subscribed mouse button events over the window title
  259. func (wt *WindowTitle) onMouse(evname string, ev interface{}) {
  260. mev := ev.(*window.MouseEvent)
  261. switch evname {
  262. case OnMouseDown:
  263. wt.pressed = true
  264. wt.mouseX = mev.Xpos
  265. wt.mouseY = mev.Ypos
  266. wt.win.root.SetMouseFocus(wt)
  267. case OnMouseUp:
  268. wt.pressed = false
  269. wt.win.root.SetMouseFocus(nil)
  270. default:
  271. return
  272. }
  273. wt.win.root.StopPropagation(Stop3D)
  274. }
  275. // onCursor process subscribed cursor events over the window title
  276. func (wt *WindowTitle) onCursor(evname string, ev interface{}) {
  277. cev := ev.(*window.CursorEvent)
  278. switch evname {
  279. case OnCursorEnter:
  280. wt.win.root.SetCursorDrag()
  281. case OnCursorLeave:
  282. wt.win.root.SetCursorNormal()
  283. case OnCursor:
  284. if !wt.pressed {
  285. wt.win.root.StopPropagation(Stop3D)
  286. return
  287. }
  288. dy := wt.mouseY - cev.Ypos
  289. dx := wt.mouseX - cev.Xpos
  290. wt.mouseX = cev.Xpos
  291. wt.mouseY = cev.Ypos
  292. posX := wt.win.Position().X - dx
  293. posY := wt.win.Position().Y - dy
  294. wt.win.SetPosition(posX, posY)
  295. default:
  296. return
  297. }
  298. wt.win.root.StopPropagation(Stop3D)
  299. }
  300. // applyStyles sets the specified window title style
  301. func (wt *WindowTitle) applyStyle(s *WindowStyle) {
  302. wt.SetBordersFrom(&s.TitleBorders)
  303. wt.SetBordersColor4(&s.TitleBorderColor)
  304. wt.SetColor4(&s.TitleBgColor)
  305. wt.label.SetColor4(&s.TitleFgColor)
  306. }
  307. // recalc recalculates the height and position of the label in the title bar.
  308. func (wt *WindowTitle) recalc() {
  309. xpos := (wt.width - wt.label.width) / 2
  310. wt.label.SetPositionX(xpos)
  311. wt.SetContentHeight(wt.label.Height())
  312. }