Skip to content

Commit

Permalink
Merge pull request Ziktofel#361 from MatthewMarinets/mm/custom_colours
Browse files Browse the repository at this point in the history
sc2: Adding settings to control the mapping of race to colour in the launcher GUI
  • Loading branch information
Ziktofel authored Dec 3, 2024
2 parents 12ab538 + a961a9e commit 47754d9
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 9 deletions.
15 changes: 8 additions & 7 deletions worlds/sc2/client_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ class SC2Manager(GameManager):
refresh_from_launching = True
first_check = True
first_mission = ""
button_colors: Dict[SC2Race, Tuple[float, float, float]] = {}
ctx: SC2Context

def __init__(self, ctx: SC2Context) -> None:
Expand All @@ -147,6 +148,11 @@ def on_start(self) -> None:
Window.size = window_width, window_height
for startup_warning in warnings:
logging.getLogger("Starcraft2").warning(f"Startup WARNING: {startup_warning}")
for race in (SC2Race.TERRAN, SC2Race.PROTOSS, SC2Race.ZERG):
errors, color = gui_config.get_button_color(race.name)
self.button_colors[race] = color
for error in errors:
logging.getLogger("Starcraft2").warning(f"{race.name.title()} button color setting: {error}")

def clear_tooltip(self) -> None:
if self.ctx.current_tooltip:
Expand Down Expand Up @@ -272,13 +278,8 @@ def build_mission_table(self, dt) -> None:
if mission_race == SC2Race.ANY:
mission_race = mission_obj.campaign.race
race = campaign_race_exceptions.get(mission_obj, mission_race)
racial_colors = {
SC2Race.TERRAN: (0.24, 0.84, 0.68),
SC2Race.ZERG: (1, 0.65, 0.37),
SC2Race.PROTOSS: (0.55, 0.7, 1)
}
if race in racial_colors:
mission_button.background_color = racial_colors[race]
if race in self.button_colors:
mission_button.background_color = self.button_colors[race]
mission_button.tooltip_text = tooltip
mission_button.bind(on_press=self.mission_callback)
self.mission_id_to_button[mission_id] = mission_button
Expand Down
75 changes: 73 additions & 2 deletions worlds/sc2/gui_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
Import this before importing client_gui.py to set window defaults from world settings.
"""
from .settings import Starcraft2Settings
from typing import List
from typing import List, Tuple, Any

def get_window_defaults() -> List[str]:

def get_window_defaults() -> Tuple[List[str], int, int]:
"""
Gets the window size options from the sc2 settings.
Returns a list of warnings to be printed once the GUI is started, followed by the window width and height
Expand All @@ -25,3 +26,73 @@ def get_window_defaults() -> List[str]:
window_width = Starcraft2Settings.window_width

return warnings, window_width, window_height


def validate_color(color: Any, default: Tuple[float, float, float]) -> Tuple[Tuple[str, ...], Tuple[float, float, float]]:
if isinstance(color, int):
if color < 0:
return ('Integer color was negative; expected a value from 0 to 0xffffff',), default
return (), (
((color >> 8) & 0xff) / 255,
((color >> 4) & 0xff) / 255,
((color >> 0) & 0xff) / 255,
)
elif color == 'default':
return (), default
elif color == 'white':
return (), (0.9, 0.9, 0.9)
elif color == 'black':
return (), (0.0, 0.0, 0.0)
elif color == 'grey':
return (), (0.345, 0.345, 0.345)
elif color == 'red':
return (), (0.85, 0.2, 0.1)
elif color == 'orange':
return (), (1.0, 0.65, 0.37)
elif color == 'green':
return (), (0.24, 0.84, 0.55)
elif color == 'blue':
return (), (0.3, 0.4, 1.0)
elif color == 'pink':
return (), (0.886, 0.176, 0.843)
elif not isinstance(color, list):
return (f'Invalid type {type(color)}; expected 3-element list or integer',), default
elif len(color) != 3:
return (f'Wrong number of elements in color; expected 3, got {len(color)}',), default
result: list[float] = [0.0, 0.0, 0.0]
errors: List[str] = []
expected = 'expected a number from 0 to 1'
for index, element in enumerate(color):
if isinstance(element, int):
element = float(element)
if not isinstance(element, float):
errors.append(f'Invalid type {type(element)} at index {index}; {expected}')
continue
if element < 0:
errors.append(f'Negative element {element} at index {index}; {expected}')
continue
if element > 1:
errors.append(f'Element {element} at index {index} is greater than 1; {expected}')
result[index] = 1.0
continue
result[index] = element
return tuple(errors), tuple(result)


def get_button_color(race: str) -> Tuple[Tuple[str, ...], Tuple[float, float, float]]:
from . import SC2World
baseline_color = 0.345 # the button graphic is grey, with this value in each color channel
if race == 'TERRAN':
user_color = SC2World.settings.terran_button_color
default_color = (0.0838, 0.2898, 0.2346)
elif race == 'PROTOSS':
user_color = SC2World.settings.protoss_button_color
default_color = (0.345, 0.22425, 0.12765)
elif race == 'ZERG':
user_color = SC2World.settings.zerg_button_color
default_color = (0.18975, 0.2415, 0.345)
else:
user_color = [baseline_color, baseline_color, baseline_color]
default_color = (baseline_color, baseline_color, baseline_color)
errors, color = validate_color(user_color, default_color)
return errors, tuple(x / baseline_color for x in color)
9 changes: 9 additions & 0 deletions worlds/sc2/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ class WindowHeight(int):
"""The starting height the client window in pixels"""
class GameWindowedMode(settings.Bool):
"""Controls whether the game should start in windowed mode"""
class TerranButtonColor(list):
"""Defines the colour of terran mission buttons in the launcher in rgb format (3 elements ranging from 0 to 1)"""
class ZergButtonColor(list):
"""Defines the colour of zerg mission buttons in the launcher in rgb format (3 elements ranging from 0 to 1)"""
class ProtossButtonColor(list):
"""Defines the colour of protoss mission buttons in the launcher in rgb format (3 elements ranging from 0 to 1)"""

window_width = WindowWidth(1080)
window_height = WindowHeight(720)
game_windowed_mode: Union[GameWindowedMode, bool] = False
terran_button_color = TerranButtonColor([0.0838, 0.2898, 0.2346])
zerg_button_color = ZergButtonColor([0.345, 0.22425, 0.12765])
protoss_button_color = ProtossButtonColor([0.18975, 0.2415, 0.345])

0 comments on commit 47754d9

Please sign in to comment.