Jump to content
Maintenance : Final step ×

Animated Toggle Switch - Python


Recommended Posts

  • Active Member

i saw this idea somewhere and i thought it's a little too sExYYy for me not to steal it XD

 

ui

 I've made two designs that you can choose from

Spoiler

This is the hidden content, please

This is the hidden content, please

 

 

how to use ?

The class I've created is easy to use & change for beginners

it's very similar to the CheckBox class so to use it, all u need to change ui.CheckBox() -> ui.ToggleSwitch()

and if you have any problems with "ON_UNCKECK"

self.checkBox.SetEvent(ui.__mem_func__(self.SetAllColor), "ON_UNCKECK", False)

 just correct it to

self.checkBox.SetEvent(ui.__mem_func__(self.SetAllColor), "ON_UNCHECK", False)

 

 

gif

 

giphy.gif

 

 

 

code

   Add this to your - ui.py

Spoiler
import math
MAIN_PATH = "d:/ymir work/ui/game/toggle_switch/special/"
class ToggleSwitch(Window):
	def __init__(self):
		Window.__init__(self)
		self._Initialize()
		self.CreateElements()
		
	def __del__(self):
		Window.__del__(self)
		self._Initialize()
	
	def _Initialize(self):
		self.backgroundImage = None
		self.checkImage = None
		
		self.eventFunc = { "ON_CHECK" : None, "ON_UNCHECK" : None, }
		self.eventArgs = { "ON_CHECK" : None, "ON_UNCHECK" : None, }
		
		self.isChecked = False
		self.animationProgress = 0
		self.animationDuration = 10
		self.isAnimating = False
		
		self.uncheckedXPosition = 0
		self.checkedXPosition = 13
		self.animationDistance = self.checkedXPosition - self.uncheckedXPosition
	
	def CreateElements(self):
		self.backgroundImage = ImageBox()
		self.backgroundImage.SetParent(self)
		self.backgroundImage.AddFlag("not_pick")
		self.backgroundImage.SetPosition(0, 0)
		self.backgroundImage.LoadImage(MAIN_PATH + "bg.png")
		self.backgroundImage.Show()
		
		self.checkImage = ImageBox()
		self.checkImage.SetParent(self.backgroundImage)
		self.checkImage.AddFlag("not_pick")
		self.checkImage.SetPosition(self.uncheckedXPosition, 0)
		self.checkImage.LoadImage(MAIN_PATH + "Unchecked.png")
		self.checkImage.Show()
		
		self.textInfo = TextLine()
		self.textInfo.SetParent(self)
		self.textInfo.SetPosition(0, 0)
		self.textInfo.SetWindowHorizontalAlignRight()
		self.textInfo.Show()
		
		self.SetSize(self.backgroundImage.GetWidth(), self.backgroundImage.GetHeight())
		
		self.backgroundImage.SetWindowHorizontalAlignCenter()
		
	def SetTextInfo(self, info, x = 50, y = 0):
		if self.textInfo:
			self.textInfo.SetText(info)
			self.textInfo.SetPosition(x, y)
		
	def SetCheckStatus(self, flag):
		if flag != self.isChecked:
			self.isChecked = flag
			self.StartAnimation()
	
	def GetCheckStatus(self):
		if self.checkImage:
			return self.isChecked
			
		return False
		
	def SetEvent(self, func, *args) :
		result = self.eventFunc.has_key(args[0])		
		if result :
			self.eventFunc[args[0]] = func
			self.eventArgs[args[0]] = args
		else :
			print "[ERROR] ui.py SetEvent, Can`t Find has_key : %s" % args[0]
		
	def OnMouseLeftButtonUp(self):
		if not self.isAnimating:
			self.isChecked = not self.isChecked
			self.StartAnimation()

			if self.isChecked and self.eventFunc["ON_CHECK"]:
				apply(self.eventFunc["ON_CHECK"], self.eventArgs["ON_CHECK"])
			elif not self.isChecked and self.eventFunc["ON_UNCHECK"]:
				apply(self.eventFunc["ON_UNCHECK"], self.eventArgs["ON_UNCHECK"])

	def StartAnimation(self):
		self.isAnimating = True
		self.animationProgress = 0
		self.checkImage.Show()

	def OnRender(self):
		if self.isAnimating:
			self.animationProgress += 1
			progress = float(self.animationProgress) / self.animationDuration

			if progress <= 1:
				x_offset = int(math.sin(progress * math.pi / 2) * self.animationDistance)
				
				if self.isChecked:
					self.checkImage.SetPosition(self.uncheckedXPosition + x_offset, 0)
					self.checkImage.LoadImage(MAIN_PATH + "Checked.png")
				else:
					self.checkImage.SetPosition(self.checkedXPosition - x_offset, 0)
					self.checkImage.LoadImage(MAIN_PATH + "Unchecked.png")
			else:
				self.isAnimating = False
				self.animationProgress = 0
				
				if self.isChecked:
					self.checkImage.SetPosition(self.checkedXPosition, 0)
					self.checkImage.LoadImage(MAIN_PATH + "Checked.png")
				else:
					self.checkImage.SetPosition(self.uncheckedXPosition, 0)
					self.checkImage.LoadImage(MAIN_PATH + "Unchecked.png")

 

 

 

 

Ciao

  • Metin2 Dev 64
  • Scream 1
  • Good 3
  • Love 2
  • Love 17
Link to comment
Share on other sites

On 10/21/2024 at 3:37 PM, m2hosting said:

Please be kind enough to share gratefully.

Hey @m2hosting! Essentially this was all I've changed from the original system.

From:

		if app.ENABLE_REFINE_RENEWAL:
			self.checkBox = ui.CheckBox()
			self.checkBox.SetParent(self)
			self.checkBox.SetPosition(0, 90)
			self.checkBox.SetWindowHorizontalAlignCenter()
			self.checkBox.SetWindowVerticalAlignBottom()
			self.checkBox.SetEvent(ui.__mem_func__(self.AutoRefine), "ON_CHECK", True)
			self.checkBox.SetEvent(ui.__mem_func__(self.AutoRefine), "ON_UNCKECK", False)
			self.checkBox.SetCheckStatus(constInfo.IS_AUTO_REFINE)
			self.checkBox.SetTextInfo("Keep window open.")
			self.checkBox.Show()

To:

		if app.ENABLE_REFINE_RENEWAL:
			self.checkBox = ui.ToggleSwitch()
			self.checkBox.SetParent(self)
			self.checkBox.SetPosition(-55, 70)
			self.checkBox.SetWindowHorizontalAlignCenter()
			self.checkBox.SetWindowVerticalAlignBottom()
			self.checkBox.SetEvent(ui.__mem_func__(self.AutoRefine), "ON_CHECK", True)
			self.checkBox.SetEvent(ui.__mem_func__(self.AutoRefine), "ON_UNCHECK", False)
			self.checkBox.SetCheckStatus(constInfo.IS_AUTO_REFINE)
			self.checkBox.SetTextInfo("Keep window open.", -10, 0)
			self.checkBox.Show()

The ui.CheckBox() -> ui.ToggleSwitch() just how @ CONTROL said, changed position of the switch and added position in front of the text so that it would "detach" from the switch, probably an easier way of doing this, but that's how I did it.

Edited by KronosG
Link to comment
Share on other sites

×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.