aboutsummaryrefslogtreecommitdiff
path: root/main.gd
blob: 393d6f0117547a048585511646a612c313a40964 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
extends Control

var power_throttle_timeout	: float
var keyboard_height_offset	: int
var themes					:= [
	preload("res://themes/day.tres") as Theme,
	preload("res://themes/night.tres") as Theme
]

onready var settings		:= Settings.new()
onready var popup			:= get_node("/root/main/popup") as ModalPopup
onready var file_picker		:= get_node("/root/main/file_picker") as FileDialog
onready var stage			:= get_node("/root/main/stage") as Stage
onready var database		:= get_node("/root/main/database") as Database
onready var controls_sensible_to_keyboard := [
	self,
	file_picker,
]


func _init():
	Physics2DServer.set_active(false)
	PhysicsServer.set_active(false)
	
	if OS.get_name() == "Android":
		var permissions := Array(OS.get_granted_permissions())
		if permissions.has("android.permission.READ_EXTERNAL_STORAGE") == false \
			or permissions.has("android.permission.WRITE_EXTERNAL_STORAGE") == false:
			OS.request_permissions()


func _ready():
	Input.set_use_accumulated_input(false)
	apply_theme(settings.get_value("theme_index", 0))
	keyboard_height_offset = OS.get_virtual_keyboard_height()


func _process(delta: float):
	var keyboard_height: int = OS.get_virtual_keyboard_height()
	keyboard_height = max(0, keyboard_height - keyboard_height_offset)
	for it in controls_sensible_to_keyboard:
		it.margin_bottom = -keyboard_height
	
	if power_throttle_timeout > 0.0:
		power_throttle_timeout -= delta
	else:
		Engine.target_fps = 10


func _input(event: InputEvent):
	Engine.target_fps = 0
	power_throttle_timeout = 3.5


func _unhandled_input(event: InputEvent):
	Engine.target_fps = 0
	power_throttle_timeout = 3.5
	
	if event is InputEventKey && event.is_echo() == false && event.is_pressed() && event.scancode == KEY_ESCAPE:
		_notification(MainLoop.NOTIFICATION_WM_GO_BACK_REQUEST)


func _notification(what: int):
	if what == MainLoop.NOTIFICATION_WM_GO_BACK_REQUEST:
		if popup.visible:
			popup.call_deferred("dismiss")
		elif file_picker.visible:
			file_picker.hide()
		elif stage.visible:
			stage.discard_action()
		elif database.selected_idx != -1:
			database.clear_selection()
		else:
			get_tree().quit()


func toggle_theme():
	var theme_idx := settings.get_value("theme_index", 0) as int
	var next_theme_idx = (theme_idx + 1) % themes.size()
	apply_theme(next_theme_idx)
	settings.set_value("theme_index", next_theme_idx)


func apply_theme(theme_idx: int) -> void:
	self.theme = themes[theme_idx]
	VisualServer.set_default_clear_color(get_color("background", "theme"))