pm_gtk.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. # THIS FILE IS A PART OF VCStudio
  2. # PYTHON 3
  3. import os
  4. import datetime
  5. # GTK module ( Graphical interface
  6. import gi
  7. gi.require_version('Gtk', '3.0')
  8. from gi.repository import Gtk
  9. from gi.repository import Gdk
  10. from gi.repository import GLib
  11. import cairo
  12. import threading
  13. # Own modules
  14. from settings import settings
  15. from settings import talk
  16. from project_manager import pm_project
  17. from project_manager import pm_mainLayer
  18. from project_manager import pm_newprojectLayer
  19. from project_manager import pm_scanLayer
  20. from project_manager import pm_helpLayer
  21. from project_manager import pm_updateLayer
  22. from project_manager import update_reader
  23. from project_manager import pm_installUpdatesLayer
  24. from project_manager import pm_settingsLayer
  25. from troubleshooter import error_notify
  26. # UI modules
  27. from UI import UI_testing
  28. from UI import UI_color
  29. from UI import UI_elements
  30. def previous(win):
  31. win.previous = {}
  32. for i in win.current:
  33. if type(win.current[i]) == list or type(win.current[i]) is dict:
  34. win.previous[i] = win.current[i].copy()
  35. else:
  36. win.previous[i] = win.current[i]
  37. # OK let's make a window
  38. def run():
  39. # In the Blender-Organizer I was putting the version into the title. Not cool.
  40. # Because if you would snap it to the sidebar in Ubuntu. On mouse over it would
  41. # show the first ever version. So there will be a better way to see version.
  42. # I think let's do that like in Blender. Drawn with in the window somewhere.
  43. # Setting up the window
  44. win = Gtk.Window()
  45. win.set_default_size(1200,720)
  46. win.set_position(Gtk.WindowPosition.CENTER)
  47. # I used to do win.maximize() here. But people complained that the software is slow.
  48. # so Instead I gonna keep it relativelly small when you open it. And slow only if you
  49. # go full screen.
  50. # I might figure out how to make it faster with various resulutions. Not now.
  51. win.connect("destroy", Gtk.main_quit)
  52. win.set_title("FeeGILE")
  53. win.set_default_icon_from_file("tinyicon.png")
  54. # Setting up the events ( mouse and keyboard handling )
  55. win.connect("button-press-event", mouse_button_press, win)
  56. win.connect("button-release-event", mouse_button_release, win)
  57. win.connect("key-press-event", key_press, win)
  58. win.connect("key-release-event", key_release, win)
  59. # Guess what. The entire time on Blender-Organizer 4 ( 2018 -2020 ) and
  60. # few days of trying connecting the scroll event directly to window or to
  61. # the drawing area. And just now I finally made it work. BY DOING THIS
  62. # Why scroll event is only on ScrolledWindow ? OMG !!!
  63. scroll = Gtk.ScrolledWindow()
  64. scroll.connect("scroll-event", scrolling, win)
  65. # Setting up the global variables. (kinda)
  66. win.animations = {}
  67. win.previous = {}
  68. win.current = {}
  69. win.images = {}
  70. win.imageload = 0
  71. win.text = {}
  72. win.textactive = ""
  73. win.scroll = {}
  74. win.FPS = 0
  75. win.url = "project_manager"
  76. win.update = {"versions":{}}
  77. win.projects = {}
  78. win.calllayer = False
  79. win.blink = False
  80. t2 = threading.Thread(target=update_reader.get_update_info, args=(win,))
  81. t2.start()
  82. if not settings.read("Username"):
  83. settings.write("Username", "VCStudio-User")
  84. # Cashed tables
  85. win.color = UI_color.get_table()
  86. win.settings = settings.load_all()
  87. # Default values
  88. win.current["frame"] = 0
  89. win.current["testing"] = False
  90. win.current["LMB"] = False
  91. win.current["MMB"] = False
  92. win.current["RMB"] = False
  93. win.current["keys"] = []
  94. win.current["key_letter"] = ""
  95. win.current["scroll"] = [0,0]
  96. win.current["project"] = ""
  97. win.current["calls"] = {} # Calls. See sutdio/studio_dialogs.py
  98. previous(win)
  99. # Version of the software
  100. win.version = 0.0
  101. try:
  102. vfile = open("settings/update.data")
  103. vfile = vfile.read()
  104. vfile = vfile.split("\n")
  105. for line in vfile:
  106. if line.startswith("VERSION "):
  107. win.version = float(line.replace("VERSION ", ""))
  108. break
  109. except:
  110. win.version = 0.0
  111. # FPS
  112. win.sFPS = datetime.datetime.now()
  113. # Setting the drawable
  114. pmdraw = Gtk.DrawingArea()
  115. pmdraw.set_size_request(0, 0)
  116. scroll.set_size_request(0, 0) # This step is because GTK developers are
  117. win.add(scroll) # well. A good, nice, people who knows
  118. scroll.add_with_viewport(pmdraw) # what they are doing. Really.
  119. pmdraw.connect("draw", pmdrawing, win)
  120. #run
  121. win.show_all()
  122. Gtk.main()
  123. def pmdrawing(pmdrawing, main_layer, win):
  124. # This function draws the actuall image. I'm doing full frames redraws. It's
  125. # a bit simpler then making some kind of dynamic draw call system that might
  126. # be used in such an application. But to hell with it. I did the same on the
  127. # Blender-Organizer altho with way less cairo. And it works well enought.
  128. try:
  129. # FPS counter
  130. win.fFPS = datetime.datetime.now()
  131. win.tFPS = win.fFPS - win.sFPS
  132. if win.current["frame"] % 5 == 0:
  133. win.blink = not win.blink # Iterating the blink
  134. if win.current["frame"] % 10 == 0:
  135. win.FPS = int ( 1.0 / ( win.tFPS.microseconds /1000000))
  136. if "Auto_De-Blur" not in win.settings:
  137. win.settings["Auto_De-Blur"] = True
  138. # Fail switch for Graphics.
  139. if win.FPS < 10 and win.settings["Auto_De-Blur"]:
  140. win.settings["Blur"] = False
  141. win.sFPS = datetime.datetime.now()
  142. win.cursors = {
  143. "arrow":Gdk.Cursor.new(Gdk.CursorType.ARROW),
  144. "watch":Gdk.Cursor.new(Gdk.CursorType.WATCH),
  145. "text" :Gdk.Cursor.new(Gdk.CursorType.XTERM),
  146. "hand" :Gdk.Cursor.new(Gdk.CursorType.HAND1),
  147. "cross":Gdk.Cursor.new(Gdk.CursorType.CROSS)
  148. }
  149. win.current["cursor"] = win.cursors["arrow"]
  150. # CHECKING FOR UPDATES
  151. # I don't want updates to be checking automatically if the user
  152. # doesn't want to. I think by default they will be automatic. But
  153. # you can definitely argue with me about it.
  154. if "check-for-updates" not in win.settings:
  155. settings.write("check-for-updates", True)
  156. win.settings["check-for-updates"] = True
  157. if win.settings["check-for-updates"]:
  158. GLib.timeout_add(1 , update_reader.get_update_info, win)
  159. # Current frame (for animations and things like this)
  160. win.current["frame"] += 1
  161. # Getting data about the frame
  162. win.current['mx'] = win.get_pointer()[0]
  163. win.current['my'] = win.get_pointer()[1]
  164. win.current['w'] = win.get_size()[0]
  165. win.current['h'] = win.get_size()[1]
  166. #Background color
  167. UI_color.set(main_layer, win, "background")
  168. main_layer.rectangle(
  169. 0,
  170. 0,
  171. win.current['w'],
  172. win.current['h'])
  173. main_layer.fill()
  174. # Tooltips and other junk has to be defined here. And then drawn later to
  175. # the screen. So here we get a special layer. That will be drawn to during
  176. # the time of drawing. And later composeted over everything.
  177. win.tooltip_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, win.current['w'],
  178. win.current['h'])
  179. win.tooltip = cairo.Context(win.tooltip_surface)
  180. win.tooltip.select_font_face("Monospace", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
  181. # Layers. Order of them matter
  182. Layers = []
  183. Layers.append([pm_mainLayer.layer(win),"project_manager"])
  184. # Call layers. See studio/studio_dialogs.py for explanation. It's wild.
  185. win.calllayer = False
  186. remlater = []
  187. for call in list(win.current["calls"].keys()):
  188. if win.current["calls"][call]["var"] == None:
  189. Layers.append([win.current["calls"][call]["draw"](win, call)])
  190. win.url = win.current["calls"][call]["url"]
  191. win.calllayer = True
  192. else:
  193. win.url = win.current["calls"][call]["back"]
  194. win.current["calls"][call]["call"](win, win.current["calls"][call]["var"])
  195. win.textactive = ""
  196. remlater.append(call)
  197. for call in remlater:
  198. del win.current["calls"][call]
  199. if win.url == "new_project":
  200. Layers.append([pm_newprojectLayer.layer(win), "new_project"])
  201. elif win.url == "scan_projects":
  202. Layers.append([pm_scanLayer.layer(win), "scan_projects"])
  203. elif win.url == "help_layer":
  204. Layers.append([pm_helpLayer.layer(win), "help_layer"])
  205. elif win.url == "update_layer":
  206. Layers.append([pm_updateLayer.layer(win), "update_layer"])
  207. elif win.url == "install_updates":
  208. Layers.append([pm_installUpdatesLayer.layer(win), "install_updates"])
  209. elif win.url == "settings_layer":
  210. Layers.append([pm_settingsLayer.layer(win), "settings_layer"])
  211. Layers.append([UI_testing.layer(win)])
  212. Layers.append([win.tooltip_surface])
  213. # Combining layers
  214. for layer in Layers:
  215. if len(layer) > 1:
  216. layer, url = layer
  217. blur = UI_elements.animate(url+"_blur", win, 50)
  218. if win.url != url:
  219. blur = UI_elements.animate(url+"_blur", win, blur, 50, 2, True)
  220. else:
  221. blur = UI_elements.animate(url+"_blur", win, blur, 0, 2, True)
  222. layer = UI_elements.blur(layer, win, blur)
  223. else:
  224. layer = layer[0]
  225. main_layer.set_source_surface(layer, 0 , 0)
  226. main_layer.paint()
  227. win.get_root_window().set_cursor(win.current["cursor"])
  228. # If you press ESC you get back from any window to the main menu.
  229. if 65307 in win.current["keys"] and win.url != "install_updates":
  230. win.url = "project_manager"
  231. win.current["project"] = ""
  232. win.textactive = ""
  233. # Saving data about this frame for the next one. A bit hard to get WTF am I
  234. # doing here. Basically trying to avoid current and previous data to be links
  235. # of the same data.
  236. previous(win) # Moved it into a seprate function for obvoius reasons
  237. # Refreshing those that need to be refrashed
  238. win.current["scroll"] = [0,0]
  239. # Refreshing the frame automatically
  240. pmdrawing.queue_draw()
  241. except:
  242. Gtk.main_quit()
  243. error_notify.show()
  244. # This program will have things like mouse and keyboard input. And this setup
  245. # Will be done in both PM and the actuall Project window. ( Also in the render
  246. # Window. Basically all separate windows will have to have this setup separatelly.
  247. # Mouse
  248. def mouse_button_press(widget, event, win):
  249. # This function marks activation of the button. Not it's deactivation.
  250. # I'm going to attempt something quite disturbing. Basically I want to save
  251. # the state of the mouse as the press begun untill it's released. And I'm
  252. # going to do in a slightly weird way. Because I'm bored I guess. The prob-
  253. # lem is that it will require to check whether the data even exists in the
  254. # first place. If x. Before parsing it. Because it might be False.
  255. for i, button in enumerate(["LMB", "MMB", "RMB"]):
  256. if i+1 == int(event.get_button()[1]):
  257. win.current[button] = [event.x, event.y]
  258. # If you folowed the code. By checking for example if win.current["LMB"]
  259. # You can know if it's even pressed to begin with. Because if it's not
  260. # It's False.
  261. def mouse_button_release(widget, event, win):
  262. # This function reverses the effects of the mouse_button_press() function.
  263. for i, button in enumerate(["LMB", "MMB", "RMB"]):
  264. if i+1 == int(event.get_button()[1]):
  265. win.current[button] = False
  266. # I guess it's time to make something similar for the keyboard keys as well.
  267. # I'm going to reuse the old system from the Blender-Organizer. Just a list of
  268. # pressed keys. Maybe as well a strting thingy. Because I want to type in this
  269. # app.
  270. def key_press(widget, event, win):
  271. if event.keyval not in win.current["keys"]:
  272. win.current["keys"].append(event.keyval)
  273. win.current["key_letter"] = event.string
  274. def key_release(widget, event, win):
  275. try:
  276. win.current["keys"].remove(event.keyval)
  277. except:
  278. win.current["keys"] = []
  279. def scrolling(widget, event, win):
  280. e, x, y = event.get_scroll_deltas()
  281. win.current["scroll"] = [x,y]