All files / blacktrigram/src/utils pixiExtensions.ts

0% Statements 0/119
100% Branches 1/1
100% Functions 1/1
0% Lines 0/119

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                                                                                                                                                                       
/**
 * PixiJS extensions and utilities for Black Trigram
 */
import { extend, useTick } from "@pixi/react";
import * as PIXI from "pixi.js";
import { Container, Graphics, Sprite, Text } from "pixi.js";
 
/**
 * Extend PIXI components for use with React
 */
export const extendPixiComponents = () => {
  extend({ Container, Graphics, Text, Sprite });
};
 
/**
 * Hook to use PixiJS extensions
 */
export const usePixiExtensions = () => {
  // Extend PIXI components on first call
  extendPixiComponents();
 
  return {
    extendPixiComponents,
  };
};
 
// Export useTick from @pixi/react for convenience
export { useTick };
 
// Default export for backward compatibility
export default usePixiExtensions;
 
/**
 * Create a PIXI.TextStyle with appropriate fallbacks
 */
export const createTextStyle = (
  style: Partial<PIXI.TextStyleOptions>
): PIXI.TextStyle => {
  return new PIXI.TextStyle(style);
};
 
/**
 * Create a responsive text style based on screen width
 */
export const createResponsiveTextStyle = (
  baseStyle: Partial<PIXI.TextStyleOptions>,
  screenWidth: number
): PIXI.TextStyle => {
  const isMobile = screenWidth < 768;
  const isTablet = screenWidth >= 768 && screenWidth < 1024;
 
  // Adjust font size based on screen size
  const fontSize = baseStyle.fontSize as number;
  const responsiveFontSize = isMobile
    ? fontSize * 0.7
    : isTablet
    ? fontSize * 0.85
    : fontSize;
 
  return new PIXI.TextStyle({
    ...baseStyle,
    fontSize: responsiveFontSize,
  });
};
 
/**
 * Draw a simple rounded button
 */
export const drawButton = (
  g: PIXI.Graphics,
  width: number,
  height: number,
  options: {
    fillColor?: number;
    strokeColor?: number;
    strokeWidth?: number;
    alpha?: number;
    cornerRadius?: number;
  } = {}
) => {
  const {
    fillColor = 0x333333,
    strokeColor = 0x666666,
    strokeWidth = 2,
    alpha = 1,
    cornerRadius = 8,
  } = options;
 
  g.clear();
  g.fill({ color: fillColor, alpha });
  g.roundRect(0, 0, width, height, cornerRadius);
  g.fill();
 
  if (strokeWidth > 0) {
    g.stroke({ width: strokeWidth, color: strokeColor, alpha });
    g.roundRect(0, 0, width, height, cornerRadius);
    g.stroke();
  }
};
 
/**
 * Draw a Korean-style panel
 */
export const drawKoreanPanel = (
  g: PIXI.Graphics,
  width: number,
  height: number,
  options: {
    fillColor?: number;
    borderColor?: number;
    borderWidth?: number;
    alpha?: number;
    cornerRadius?: number;
  } = {}
) => {
  const {
    fillColor = 0x1a1a2e,
    borderColor = 0x00ffff,
    borderWidth = 2,
    alpha = 0.9,
    cornerRadius = 8,
  } = options;
 
  g.clear();
  g.fill({ color: fillColor, alpha });
  g.roundRect(0, 0, width, height, cornerRadius);
  g.fill();
 
  g.stroke({ width: borderWidth, color: borderColor, alpha: 0.8 });
  g.roundRect(0, 0, width, height, cornerRadius);
  g.stroke();
};
 
/**
 * Enhanced Graphics API wrapper for v8 compatibility
 */
export const createKoreanGraphics = () => {
  const graphics = new Graphics();
 
  // Modern PixiJS v8 API wrappers
  const drawRoundedRect = (
    x: number,
    y: number,
    width: number,
    height: number,
    radius: number
  ) => {
    return graphics.roundRect(x, y, width, height, radius); // Updated API
  };
 
  const fillWithColor = (color: number, alpha = 1) => {
    return graphics.fill({ color, alpha }); // Updated API - no beginFill/endFill needed
  };
 
  const strokeWithColor = (color: number, width = 1, alpha = 1) => {
    return graphics.stroke({ color, width, alpha }); // Updated API
  };
 
  return {
    graphics,
    drawRoundedRect,
    fillWithColor,
    strokeWithColor,
  };
};
 
/**
 * Korean martial arts specific drawing utilities
 */
export const drawTrigramSymbol = (
  graphics: Graphics,
  x: number,
  y: number,
  size: number
) => {
  graphics.clear();
 
  // Draw trigram lines using modern API
  graphics.rect(x, y, size, size / 8);
  graphics.fill({ color: 0x00ffff });
 
  graphics.rect(x, y + size / 3, size, size / 8);
  graphics.fill({ color: 0x00ffff });
 
  graphics.rect(x, y + (2 * size) / 3, size, size / 8);
  graphics.fill({ color: 0x00ffff });
};