Example #1
0
/**
 * Re-use the template state, to avoid re-initialization of the buffer,
 * which requires additional operations which, summed up, may lead to
 * a "Script too Complex" error
 */
                                 public MyMatrixColumnSprite FromTemplate(MyStatefulAnimatedSprite Template)
                                 {
                                     WithState("Default", Template.GetState("Default"));
                                     return(this);
                                 }
Example #2
0
                                 public MyChaseLightController(
                                     IMyGridTerminalSystem GridTerminalSystem,
                                     string textPanelName,
                                     bool mirrored,
                                     int operatingMode,
                                     int movementSpeed,
                                     MySprite[] ChaseLightShapeFrames
                                     )
                                 {
                                     // Sanity check
                                     if (textPanelName == null || textPanelName.Length == 0)
                                     {
                                         throw new ArgumentException("The name of the text panel must not be at least one character long");
                                     }

                                     if (ChaseLightShapeFrames == null || ChaseLightShapeFrames.Length == 0)
                                     {
                                         throw new ArgumentException("The ChaseLightShapeFrames array must have at least one element");
                                     }

                                     if (operatingMode < OP_MODE_LEFT_TO_RIGHT || operatingMode > OP_MODE_BOUNCE_START_FROM_RIGHT)
                                     {
                                         throw new ArgumentException("The operating mode must have one of the following values: OP_MODE_LEFT_TO_RIGHT, OP_MODE_RIGHT_TO_LEFT, OP_MODE_BOUNCE_START_FROM_LEFT, OP_MODE_BOUNCE_START_FROM_RIGHT");
                                     }

                                     if (movementSpeed < 1 || movementSpeed > 10)
                                     {
                                         throw new ArgumentException("The movement speed must be between 1 and 10");
                                     }

                                     // Set up the text panel
                                     TerminalUtils.SetupTextSurfaceForMatrixDisplay(GridTerminalSystem, textPanelName, 0, FONT_SIZE);

                                     // Initialize the application
                                     OnScreenApplication
                                         = UiFrameworkUtils.InitSingleScreenApplication(
                                               GridTerminalSystem, textPanelName, 0, // Reference to the target text panel
                                               RES_X, RES_Y,                         // The target display resolution
                                               mirrored                              // The screen image might have to be mirrored
                                               );

                                     // Create the main page and add it to the application
                                     MyPage MainPage = new MyPage();

                                     OnScreenApplication.AddPage(MainPage);

                                     // Create the ChaseLightShape with only one state, named "Default",
                                     // having the referenced frames array as its animation
                                     ChaseLightShape = new MyStatefulAnimatedSprite(0, 0)
                                                       .WithState("Default", new MyStatefulAnimatedSpriteState(ChaseLightShapeFrames));

                                     // Add the ChaseLightShape to the main page
                                     MainPage.AddChild(ChaseLightShape);

                                     // Set the movement vector
                                     if (operatingMode == OP_MODE_RIGHT_TO_LEFT || operatingMode == OP_MODE_BOUNCE_START_FROM_RIGHT)
                                     {
                                         movementVector = -movementSpeed;
                                     }
                                     else
                                     {
                                         movementVector = movementSpeed;
                                     }

                                     // Set the client cycle method to the chase light shape according to the referenced operating mode
                                     ChaseLightShape.WithClientCycleMethod((MyOnScreenObject Obj, int currFrameIndex) => {
                    // Center vertically (each frame might have a different height,
                    // so this is required to run on every frame)
                    Obj.y = (RES_Y - Obj.GetHeight()) / 2;

                    // Move
                    Obj.x += movementVector;

                    // Apply the proper action for when the object goes off-screen,
                    // according to the set operating mode
                    if (operatingMode == OP_MODE_RIGHT_TO_LEFT)
                    {
                        // If it's right to left, then the objects exits through the
                        // left side and enters through the right side of the screen.
                        if (Obj.x < 0)
                        {
                            Obj.x = RES_X - 1 - Obj.GetWidth();
                        }
                    }
                    else if (
                        operatingMode == OP_MODE_BOUNCE_START_FROM_LEFT ||
                        operatingMode == OP_MODE_BOUNCE_START_FROM_RIGHT
                        )
                    {
                        // If it's bouncing, then the object's vector has to be switched
                        // whenever it reches one side or the other.
                        if (Obj.x < 0 || Obj.x + Obj.GetWidth() > RES_X - 1)
                        {
                            movementVector = -movementVector;
                        }
                    }
                    else
                    {
                        // The default is OP_MODE_LEFT_TO_RIGHT.
                        // In this case, the objects exits the screen through the right side
                        // and enters through the left side.
                        if (Obj.x + Obj.GetWidth() > RES_X - 1)
                        {
                            Obj.x = 0;
                        }
                    }
                });
                                 }