Home>Python Tips>

しなちく設定クローン

更新日:2003/10/22

しなちく付属設定ツールのクローンをPythonで作ってみました。wxPythonの取っかかりとしても使えるかも。


kutinashi.py

# -*- coding: japanese.shift_jis -*-

# Python         - http://www.python.org/
# JapaneseCodecs - http://www.python.jp/Zope/download/JapaneseCodecs/
# ctypes         - http://starship.python.net/crew/theller/ctypes/
# wxPython       - http://wxpython.org/

from wx import *
from ctypes import *
import string

# リソースID
IDC_BUTTON_OK = 1001
IDC_BUTTON_CANCEL = 1002

# sipc.dll helper
class Sipc:
	def __init__(self, name):
		self.sipc = windll.LoadLibrary('sipc.dll')
		self.sipc.Connect(name)
	
	def send(self, command):
		buf = c_buffer(1024)
		self.sipc.Send(command, buf, sizeof(buf))
		return buf.value


class RGB:
	def __init__(self, rgb=0):
		self.r = 0x000000ff & rgb
		self.g = 0x000000ff & (rgb >> 8)
		self.b = rgb >> 16


class SettingDialog(Dialog):
	def __init__(self, parent=None, id=-1, title=u'設定',
			pos=DefaultPosition, size=(351, 314)):
		"""コンストラクタ"""
		Dialog.__init__(self, parent, id, title, pos, size)

		self.InitializeComponent()

		self.sipc = Sipc('kutinashi')
		self.LoadValues()

	def LoadValues(self):
		"""設定を読み込む"""
		# 喋る間隔
		v = string.atoi(self.sipc.send('sv_autosayinterval'))
		self.text1.SetValue('%ld' % (v / (1000 * 60)))

		# サーバ問い合わせ間隔
		v = string.atoi(self.sipc.send('sv_autoreloadinterval'))
		self.text2.SetValue('%ld' % (v / (1000 * 60)))

		# 吹き出しが閉じるまでの時間
		v = string.atoi(self.sipc.send('sv_scriptorautohidetime'))
		self.text3.SetValue('%ld' % (v / 1000))

		# コンソール背景色
		v = string.atoi(self.sipc.send('sc_gui_consolebackcolor'))
		rgb = RGB(v)
		r = self.slider1R.SetValue(rgb.r)
		g = self.slider1G.SetValue(rgb.g)
		b = self.slider1B.SetValue(rgb.b)

		# コンソール文字色
		v = string.atoi(self.sipc.send('sc_gui_consolefontcolor'))
		rgb = RGB(v)
		self.slider2R.SetValue(rgb.r)
		self.slider2G.SetValue(rgb.g)
		self.slider2B.SetValue(rgb.b)

		# コンソール透明度
		v = string.atoi(self.sipc.send('sc_gui_consolealpha'))
		self.slider3.SetValue(v)

		# スクリプタ透明度
		v = string.atoi(self.sipc.send('sc_gui_scriptoralpha'))
		self.slider4.SetValue(v)

		# テクスチャ透明度
		v = string.atoi(self.sipc.send('sc_gui_texturealpha'))
		self.slider5.SetValue(v)

	def StoreValues(self):
		"""設定を反映させる"""
		# 喋る間隔(分)
		v = string.atoi(self.text1.GetValue())
		if v < 0: v = 1
		self.sipc.send('sv_autosayinterval %ld' % (v * 60 * 1000))

		# サーバ問い合わせ間隔(分)
		v = string.atoi(self.text2.GetValue())
		if v < 0: v = 1
		self.sipc.send('sv_autoreloadinterval %ld' % (v * 60 * 1000))

		# 吹き出しが閉じるまでの時間(秒)
		v = string.atoi(self.text3.GetValue())
		if v < 0: v = 1
		self.sipc.send('sv_scriptorautohidetime %ld' % (v * 1000))

		# コンソール背景色
		r = self.slider1R.GetValue()
		g = self.slider1G.GetValue()
		b = self.slider1B.GetValue()
		self.sipc.send('setguiattribute console backcolor %ld %ld %ld' % (r,g,b))

		# コンソール文字色
		r = self.slider2R.GetValue()
		g = self.slider2G.GetValue()
		b = self.slider2B.GetValue()
		self.sipc.send('setguiattribute sonsole fontcolor %ld %ld %ld' % (r,g,b))

		# コンソール透明度
		a = self.slider3.GetValue()
		self.sipc.send('setguiattribute console alpha %ld' % (a))

		# スクリプタ透明度
		a = self.slider4.GetValue()
		self.sipc.send('setguiattribute scriptor alpha %ld' % (a))

		# テクスチャ透明度
		a = self.slider5.GetValue()
		self.sipc.send('setguiattribute texture alpha %ld' % (a))

	def InitializeComponent(self):
		"""コンポーネントの配置を行う"""
		btnOK = Button(self, IDC_BUTTON_OK, u'OK')
		btnOK.SetPosition(Point(185, 258))
		btnOK.SetDefault()
		btnCancel = Button(self, IDC_BUTTON_CANCEL, u'Cancel')
		btnCancel.SetPosition(Point(263, 258))

		book = Notebook(self, -1, pos=(8, 10), size=(329,241))

		# Page1
		page1 = NotebookPage(book, -1)
		book.AddPage(page1, u'基本')
		StaticText(page1, -1, u'喋る間隔(&S)', pos=(8, 12))
		StaticText(page1, -1, u'サーバ問い合わせ間隔(&R)', pos=(8, 36))
		StaticText(page1, -1, u'吹き出しが閉じるまでの時間(&C)', pos=(8, 76))
		StaticText(page1, -1, u'約', pos=(216, 12))
		StaticText(page1, -1, u'約', pos=(216, 36))
		StaticText(page1, -1, u'約', pos=(216, 76))
		StaticText(page1, -1, u'分', pos=(296, 12))
		StaticText(page1, -1, u'分', pos=(296, 36))
		StaticText(page1, -1, u'秒', pos=(296, 76))
		self.text1 = TextCtrl(page1, -1, u'', pos=(232, 8), size=(60, 20))
		self.text2 = TextCtrl(page1, -1, u'', pos=(232, 32), size=(60, 20))
		self.text3 = TextCtrl(page1, -1, u'', pos=(232, 72), size=(60, 20))

		# Page2
		page2 = NotebookPage(book, -1)
		book.AddPage(page2, u'GUI')

		# コンソール背景色
		StaticText(page2, -1, u'コンソール背景色(&C)', pos=(8, 8))
		StaticText(page2, -1, u'R', pos=(8, 32))
		StaticText(page2, -1, u'G', pos=(8, 56))
		StaticText(page2, -1, u'B', pos=(8, 80))
		
		self.slider1R = Slider(page2, -1, 0, 0, 255, (24, 28), (128, -1), 
				SL_HORIZONTAL|SL_AUTOTICKS)
		self.slider1R.SetTickFreq(16, 1)
		self.slider1G = Slider(page2, -1, 0, 0, 255, (24, 52), (128, -1), 
				SL_HORIZONTAL|SL_AUTOTICKS)
		self.slider1G.SetTickFreq(16, 1)
		self.slider1B = Slider(page2, -1, 0, 0, 255, (24, 76), (128, -1), 
				SL_HORIZONTAL|SL_AUTOTICKS)
		self.slider1B.SetTickFreq(16, 1)

		# コンソール文字色
		y = 104 
		StaticText(page2, -1, u'コンソール文字色(&F)', pos=(8, 8 + y))
		StaticText(page2, -1, u'R', pos=(8, 32 + y))
		StaticText(page2, -1, u'G', pos=(8, 56 + y))
		StaticText(page2, -1, u'B', pos=(8, 80 + y))
		
		self.slider2R = Slider(page2, -1, 0, 0, 255, (24, 28 + y), (128, -1), 
				SL_HORIZONTAL|SL_AUTOTICKS)
		self.slider2R.SetTickFreq(16, 1)
		self.slider2G = Slider(page2, -1, 0, 0, 255, (24, 52 + y), (128, -1), 
				SL_HORIZONTAL|SL_AUTOTICKS)
		self.slider2G.SetTickFreq(16, 1)
		self.slider2B = Slider(page2, -1, 0, 0, 255, (24, 76 + y), (128, -1), 
				SL_HORIZONTAL|SL_AUTOTICKS)
		self.slider2B.SetTickFreq(16, 1)

		# -
		StaticLine(page2, -1, (160, 8), (2, 216), LI_VERTICAL)

		# コンソール透明度
		StaticText(page2, -1, u'コンソール透明度(&D)', pos=(172, 8))
		StaticText(page2, -1, u'A', pos=(172, 32))
		self.slider3 = Slider(page2, -1, 0, 0, 255, (188, 28), (128, -1), 
				SL_HORIZONTAL|SL_AUTOTICKS)
		self.slider3.SetTickFreq(16, 1)

		# スクリプタ透明度
		StaticText(page2, -1, u'スクリプタ透明度(&S)', pos=(172, 64))
		StaticText(page2, -1, u'A', pos=(172, 88))
		self.slider4 = Slider(page2, -1, 0, 0, 255, (188, 84), (128, -1), 
				SL_HORIZONTAL|SL_AUTOTICKS)
		self.slider4.SetTickFreq(16, 1)

		# テクスチャ透明度
		StaticText(page2, -1, u'テクスチャ透明度(&T)', pos=(172, 120))
		StaticText(page2, -1, u'A', pos=(172, 144))
		self.slider5 = Slider(page2, -1, 0, 0, 255, (188, 140), (128, -1), 
				SL_HORIZONTAL|SL_AUTOTICKS)
		self.slider5.SetTickFreq(16, 1)
	
		# events
		EVT_CLOSE(self, self.OnClose)
		EVT_BUTTON(self, btnOK.GetId(), self.OnOK)
		EVT_BUTTON(self, btnCancel.GetId(), self.OnCancel)

	def OnOK(self, evt):
		self.StoreValues()
		self.Destroy()

	def OnClose(self, evt):
		self.Destroy()

	def OnCancel(self, evt):
		self.Destroy()
	

class MyApp(App):
	def OnInit(self):
		self.dlg = SettingDialog()
		self.dlg.Show(True)
		self.SetTopWindow(self.dlg)
		return True
	

def main():
	app = MyApp()
	app.MainLoop()


if __name__ == '__main__':
	main()

# vim:ts=4 sts=4 sw=4:
		

Home>Python Tips>