summaryrefslogtreecommitdiff
path: root/gshaderedit.in
blob: cb40f31280da09b9ab9aa4c67f8fea25a5318d3d (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python
# coding=utf-8

"""
gShaderEdit
Author : Guillaume "iXce" Seguin
Email  : guillaume@segu.in

 # Live Shader editor (mainly color related shaders) #

Copyright (C) 2007 Guillaume Seguin

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
"""

import pygtk
pygtk.require ('2.0')
import gtk
import gtk.glade

import gettext

from gShaderEdit.previewarea import previewArea

GETTEXT_DOMAIN      = "gshaderedit"
DATADIR             = "@prefix@/share"
LOCALES_DIR	    = DATADIR + "/locale"
GLADE_FILE          = DATADIR + "/gshaderedit/gshaderedit.glade"
DEFAULT_IMAGE       = DATADIR + "/gshaderedit/chromaticity.png"

gettext.install (GETTEXT_DOMAIN, LOCALES_DIR, unicode = True)
gtk.glade.bindtextdomain (GETTEXT_DOMAIN, LOCALES_DIR)

def autoconnect (ob):
    '''Autoconnect every member from ob to its glade interface'''
    handlers = {}
    for i in dir (ob):
        handlers[i] = getattr (ob, i)
    ob.glade.signal_autoconnect (handlers);

class gShaderEdit ():

    glade = None
    previewArea = None
    mainWindow = None
    aboutDialog = None
    editorView = None

    def __init__ (self):
        '''Initialize application'''
        self.glade = gtk.glade.XML (fname = GLADE_FILE, 
                                    domain = GETTEXT_DOMAIN)
        self.mainWindow = self.glade.get_widget ("mainWindow")
        self.aboutDialog = self.glade.get_widget ("aboutDialog")
        self.editorView = self.glade.get_widget ("editorView")
        autoconnect (self)
        self.editorBuffer = self.editorView.get_buffer ()
        self.editorBuffer.connect ("changed", self.source_changed)
        self.load_image (DEFAULT_IMAGE)

    def show (self):
        '''Show application'''
        self.mainWindow.show_all ()

    def gtk_main_quit (self, *args):
        '''Quit gtk main loop'''
        gtk.main_quit ()

    def show_about (self, *args):
        '''Show about dialog'''
        self.aboutDialog.show ()

    def close_about (self, *args):
        '''Hide about dialog'''
        self.aboutDialog.hide ()
        return True

    def get_text (self):
        '''Retrieve text from editor'''
        return self.editorBuffer.get_property ("text")

    def source_changed (self, *args):
        '''Source changed callback, update apply button sensitivity'''
        empty = self.get_text ().strip () == ""
        self.glade.get_widget ("applyButton").set_sensitive (not empty)
        self.glade.get_widget ("saveButton").set_sensitive (not empty)

    def apply (self, *args):
        '''Apply button callback, apply fragment shader to preview area'''
        program_source = self.get_text ().strip ()
        if program_source == "":
            print _("Warning : Empty source")
            return
        self.previewArea.create_program (program_source)
        applied = self.previewArea.program and \
                  self.previewArea.program.id.value != 0
        if not applied:
            self.previewArea.delete_program ()
        self.glade.get_widget ("resetButton").set_sensitive (applied)

    def reset (self, *args):
        '''Reset button callback, remove fragment shader from preview area'''
        self.previewArea.delete_program ()
        self.glade.get_widget ("resetButton").set_sensitive (False)

    def file_dialog (self, name, action, button, filters = None):
	dialog = gtk.FileChooserDialog (name, None, action,
					(gtk.STOCK_CANCEL,
					 gtk.RESPONSE_CANCEL,
					 button,
					 gtk.RESPONSE_OK))
	if filters:
	    map (dialog.add_filter, filters)
	dialog.set_position (gtk.WIN_POS_CENTER)
	dialog.set_icon (self.mainWindow.get_icon ())
	dialog.set_transient_for (self.mainWindow)
        dialog.set_modal (True)
	response = dialog.run ()
	file = None
	if response == gtk.RESPONSE_OK:
	    file = dialog.get_filename ()
	dialog.destroy ()
	return file

    def open_dialog (self, filters = None):
	action = gtk.FILE_CHOOSER_ACTION_OPEN
	button = gtk.STOCK_OPEN
	return self.file_dialog (_("Open file..."), action, button, filters)

    def save_dialog (self, filters = None):
	action = gtk.FILE_CHOOSER_ACTION_SAVE
	button = gtk.STOCK_SAVE
	return self.file_dialog (_("Save as..."), action, button, filters)

    def open_file (self, *args):
        '''Open a file chooser to choose a custom image file and load it'''
        filters = []
        filter = gtk.FileFilter ()
        filter.set_name (".png")
        filter.add_pattern ("*.png")
        filters += [filter]
        filter = gtk.FileFilter ()
        filter.set_name (_("All files"))
        filter.add_pattern ("*")
        filters += [filter]
	file = self.open_dialog (filters)
        if file:
            self.load_image (file)

    def open_shader (self, *args):
        '''Open a file chooser to choose a shader file and load it'''
	file = self.open_dialog ()
        if file:
	    f = open (file)
	    if not f:
		return
	    source = f.read ()
	    self.editorBuffer.set_text (source)

    def save (self, *args):
        '''Open a file chooser to choose target file and save filter'''
	file = self.save_dialog ()
        if file:
	    f = open (file, "w")
	    if not f:
		return
	    f.write (self.get_text ())
	    f.close ()

    def load_image (self, file):
        '''Load image file into the preview area (create it if needed)'''
        if not self.previewArea:
            self.previewArea = previewArea (self.mainWindow, file)
            self.glade.get_widget ("drawingFrame").add (self.previewArea)
        else:
            self.previewArea.load_texture (file)

if __name__ == "__main__":
    editor = gShaderEdit ()
    editor.show ()
    gtk.main ()