Skip to content

Commit 3ddc1d3

Browse files
committed
Project files and global configuration
1 parent d34ada4 commit 3ddc1d3

File tree

8 files changed

+318
-138
lines changed

8 files changed

+318
-138
lines changed

data/ui/main.glade

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
<!-- Generated with glade 3.20.2 -->
33
<interface>
44
<requires lib="gtk+" version="3.0"/>
5+
<menu id="appmenu">
6+
<item>
7+
<attribute name="label" translatable="yes">_Preferences</attribute>
8+
<attribute name="action">app.preferences</attribute>
9+
</item>
10+
<item>
11+
<attribute name="label" translatable="yes">_Quit</attribute>
12+
<attribute name="action">app.quit</attribute>
13+
<attribute name="accel">&lt;Primary&gt;Q</attribute>
14+
</item>
15+
</menu>
516
<menu id="menubar">
617
<submenu>
718
<attribute name="label" translatable="yes">_File</attribute>
@@ -26,14 +37,6 @@
2637
<attribute name="action">app.close</attribute>
2738
</item>
2839
</section>
29-
<section>
30-
<item>
31-
<attribute name="label" translatable="yes">_Quit</attribute>
32-
<attribute name="hidden-when">macos-menubar</attribute>
33-
<attribute name="action">app.quit</attribute>
34-
<attribute name="accel">&lt;Primary&gt;Q</attribute>
35-
</item>
36-
</section>
3740
</submenu>
3841
<submenu>
3942
<attribute name="label" translatable="yes">_Edit</attribute>
@@ -148,10 +151,6 @@
148151
<attribute name="label" translatable="yes">So_und Test</attribute>
149152
<attribute name="action">app.audio</attribute>
150153
</item>
151-
<item>
152-
<attribute name="label" translatable="yes">R_TP Directory</attribute>
153-
<attribute name="action">app.rtp</attribute>
154-
</item>
155154
<item>
156155
<attribute name="label" translatable="yes">_Open Game Directory</attribute>
157156
<attribute name="action">app.directory</attribute>

src/app.rb

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
require_relative 'tileset_view.rb'
2323
require_relative 'mapinfo_bind.rb'
2424
require_relative 'database.rb'
25-
require_relative 'rtp.rb'
2625
require_relative 'new_project.rb'
26+
require_relative 'config.rb'
2727

2828
class ApplicationWindow < Gtk::ApplicationWindow
2929
def initialize(app, builder)
@@ -69,12 +69,14 @@ def connect_store
6969
file.set_boolean("WindowState", "Maximized", @maximized)
7070
# querying while destroying is not safe, but I can't find paned's signal
7171
file.set_integer("WindowState", "PanedPosition", @paned.position)
72-
path = File.expand_path(GLib.application_name, GLib.user_cache_dir)
7372
begin
74-
FileUtils.mkdir_p(path)
75-
IO.write(File.expand_path("state.ini", path), file.to_data)
73+
dir = File.expand_path(GLib.application_name, GLib.user_cache_dir)
74+
FileUtils.mkdir_p(dir)
75+
IO.write(File.expand_path("state.ini", dir), file.to_data)
7676
rescue Errno::EACCES
7777
end
78+
# FIXME move to application close
79+
$config.save
7880
end
7981
end
8082
end
@@ -86,12 +88,15 @@ def initialize
8688
self.signal_connect("activate") { on_activate }
8789
end
8890
def on_startup
91+
$config = Configuration.new
92+
8993
@builder = ExtraBuilder.new("main")
94+
self.set_app_menu(@builder.get_object("appmenu"))
9095
self.set_menubar(@builder.get_object("menubar"))
9196

92-
add_simple_actions("open", "save", "close", "run", "quit", "undo",
93-
"redo", "copy", "cut", "paste", "delete", "scripts",
94-
"database", "rtp")
97+
add_simple_actions("open", "save", "close", "run", "quit",
98+
#"undo", "redo", "copy", "cut", "paste",
99+
"delete", "scripts", "database")
95100
add_radio_action("layerview", "all")
96101
add_radio_action("layeredit", "layer1")
97102
add_check_action("dim")
@@ -146,8 +151,13 @@ def edit_map(map_id)
146151
@palette.tileset = $project.tilesets[@map.tileset_id]
147152
end
148153
def on_open
154+
filter = Gtk::FileFilter.new
155+
filter.add_pattern("*.rxproj")
156+
filter.add_pattern("*.xproj")
157+
filter.name = "XP Project"
158+
149159
dialog = Gtk::FileChooserDialog.new(title: "Open Project", parent: @window)
150-
dialog.action = :select_folder
160+
dialog.add_filter(filter)
151161
dialog.add_button(Gtk::Stock::CANCEL, :cancel)
152162
open = dialog.add_button(Gtk::Stock::OPEN, :ok)
153163
open.style_context.add_class("suggested-action")

src/config.rb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#
2+
# This file is part of RPGXP.
3+
#
4+
# RPGXP is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published
6+
# by the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# RPGXP is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with RPGXP. If not, see <http://www.gnu.org/licenses/>.
16+
#
17+
require 'gtk3'
18+
require_relative 'widgets.rb'
19+
20+
class Configuration
21+
def initialize
22+
path = File.expand_path(GLib.application_name, GLib.user_config_dir)
23+
filepath = File.expand_path("config", path)
24+
file = ExtraKeyFile.new(filepath)
25+
@background_color = file.get_integer_list("UI", "BackgroundColor", [255, 255, 255])
26+
@none_color = file.get_integer_list("UI", "NoneColor", [127, 127, 127])
27+
@rtps = {}
28+
if file.has_group?("RTPs")
29+
for key in file.get_keys("RTPs")
30+
@rtps[key] = file.get_string("RTPs", key)
31+
end
32+
end
33+
# if there is no config file, then it's the first time opening rpgxp
34+
unless File.exists?(filepath)
35+
welcome_message
36+
end
37+
end
38+
def save
39+
dir = File.expand_path(GLib.application_name, GLib.user_config_dir)
40+
file = ExtraKeyFile.new
41+
file.set_integer_list("UI", "BackgroundColor", @background_color)
42+
file.set_integer_list("UI", "NoneColor", @none_color)
43+
for key, path in @rtps
44+
file.set_string("RTPs", key, path)
45+
end
46+
FileUtils.mkdir_p(dir)
47+
IO.write(File.expand_path("config", dir), file.to_data)
48+
end
49+
def background_color
50+
map_color(@background_color)
51+
end
52+
def none_color
53+
map_color(@none_color)
54+
end
55+
def rtp_path(rtp)
56+
if @rtps.key?(rtp)
57+
@rtps[rtp]
58+
else
59+
# TODO gui ask to add it
60+
raise "RTP of name #{rtp} was not found."
61+
end
62+
end
63+
def welcome_message
64+
msg = "Welcome to RPG XP!\nDo you want to import a standard RTP?"
65+
ask = Gtk::MessageDialog.new(message: msg, buttons_type: :none)
66+
ask.add_button("No", :cancel)
67+
ok = ask.add_button("Import RTP", :ok)
68+
ok.style_context.add_class("suggested-action")
69+
confirmed = (ask.run == :ok)
70+
ask.destroy
71+
if confirmed
72+
dialog = Gtk::FileChooserDialog.new(title: "Import RTP (directory)")
73+
dialog.action = :select_folder
74+
dialog.add_button("Cancel", :cancel)
75+
ok = dialog.add_button("Import", :ok)
76+
ok.style_context.add_class("suggested-action")
77+
if dialog.run == :ok
78+
@rtps["Standard"] = dialog.filename
79+
end
80+
dialog.destroy
81+
end
82+
end
83+
protected
84+
def map_color(color)
85+
color.map { |n| n.to_f / 255.0 }
86+
end
87+
end
88+

src/project.rb

Lines changed: 18 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
require 'scanf'
1919
require_relative 'widget_link.rb'
2020
require_relative 'convert.rb'
21+
require_relative 'xproj.rb'
2122

2223
# FIXME move somewhere else
2324
TILE_SIZE = 32
@@ -30,12 +31,19 @@ class Project
3031
attr_accessor :actors, :animations, :armors, :classes, :common_events,
3132
:enemies, :items, :mapinfos, :scripts, :skills, :states,
3233
:system, :tilesets, :troops, :weapons, :maps
33-
attr_reader :dir, :rtp
34-
def has_conf?
35-
File.exists?("#{dir}/mkxp.conf")
36-
end
37-
def initialize(dir)
38-
@dir = dir
34+
attr_reader :dir, :config
35+
def initialize(file)
36+
@dir = File.dirname(file)
37+
case File.extname(file)
38+
when ".xproj"
39+
@config = read_marshal(file)
40+
when ".rxproj"
41+
@config = XProj.new
42+
name = File.basename(file, ".rxproj")
43+
@config.load_ini(File.expand_path("#{name}.ini", dir))
44+
else
45+
raise("Invalid Project extension")
46+
end
3947
data = "#{dir}/Data"
4048
@actors = read_marshal("#{data}/Actors.rxdata")
4149
@animations = read_marshal("#{data}/Animations.rxdata")
@@ -57,22 +65,6 @@ def initialize(dir)
5765
@maps[map_id] = read_marshal(sprintf("#{data}/Map%03d.rxdata", map_id))
5866
end
5967
try_export_scripts
60-
61-
if has_conf?
62-
@rtp = slow_read_rtp
63-
else
64-
for file in %w(mkxp_linux GMGSx.sf2 mkxp.conf)
65-
FileUtils.cp("#{$DATA_DIR}/system/#{file}", dir)
66-
end
67-
FileUtils.chmod(0755, "#{dir}/mkxp_linux")
68-
69-
dialog = rtp_choose_dialog(nil, "RTP Directory")
70-
if dialog.run == :ok
71-
@rtp = dialog.filename
72-
self.rtp = @rtp
73-
end
74-
dialog.destroy
75-
end
7668
end
7769
def save
7870
# load script files into memory, then save it as .rxdata
@@ -97,6 +89,10 @@ def save
9789
@mapinfos.each do |map_id, info|
9890
write_marshal(@maps[map_id], sprintf("#{data}/Map%03d.rxdata", map_id))
9991
end
92+
# save metadata
93+
@config.save(dir)
94+
@config.export_ini(dir)
95+
@config.export_conf(dir)
10096
end
10197
def try_export_scripts
10298
export_scripts unless has_scripts?
@@ -154,46 +150,6 @@ def extract_script_path(path)
154150
def scripts_dir
155151
"#{dir}/Scripts"
156152
end
157-
def slow_read_rtp
158-
conf_read("RTP")
159-
end
160-
def rtp=(rtp_dir)
161-
conf_write("RTP", rtp_dir)
162-
end
163-
def title=(title)
164-
conf_write("windowTitle", title)
165-
end
166-
def title
167-
conf_read("windowTitle")
168-
end
169-
protected
170-
def conf
171-
return IO.read("#{dir}/mkxp.conf").lines.map { |line| line.strip }
172-
rescue
173-
return []
174-
end
175-
def conf=(lines)
176-
IO.write("#{dir}/mkxp.conf", lines.join("\n"))
177-
end
178-
def conf_index(id)
179-
self.conf.find_index do |line|
180-
line[0...id.length] == id
181-
end
182-
end
183-
def conf_read(id)
184-
index = conf_index(id)
185-
index ? conf[index][(id.length+1)..-1] : ""
186-
end
187-
def conf_write(id, value)
188-
lines = self.conf
189-
index = conf_index(id)
190-
if index
191-
lines[index][id.length+1..-1] = value
192-
else
193-
lines.push("#{id}=#{value}")
194-
end
195-
self.conf = lines
196-
end
197153
end
198154

199155
module RPG

src/resource.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ def self.list(type)
5555
end
5656
def self.path_list(type)
5757
magic = FileMagic.new(FileMagic::MAGIC_MIME)
58-
rtp = $project.rtp
59-
dirs = rtp.empty? ? [$project.dir] : [$project.dir, rtp]
58+
dirs = [$project.dir] + $project.config.rtps
6059
resources = []
6160
for dir in dirs
6261
for file in Dir["#{dir}/#{dir(type)}/*"]

src/rtp.rb

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)